I've spent som time trying to fix it, but my app works fine except for the images that don't load into my recyclerview. I tried with and without firebase database to get the images but in both case i don't get them.
However i get all my texts so i don't understand where the problem comes from. I searched for quite a time and didn't see anything that i didn't do in other tutorials/helps.
In my logcat i get the following errors :
2021-05-27 20:31:25.445 22716-22716/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2021-05-27 20:31:25.447 22716-22716/? E/Zygote: accessInfo : 1
And in the case of firebase Database i get the previous errors plus this one :
E/RecyclerView: No adapter attached; skipping layout
I put here my code so you can check if you need
Activity class :
package fr.balizok.pizzaapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import fr.balizok.pizzaapplication.adapter.PizzaAdapter;
public class MainActivity extends AppCompatActivity {
List<PizzaModel> pizzaList = new ArrayList<PizzaModel>();
RecyclerView recyclerViewPizzas;
PizzaAdapter pizzaAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pizzaList.add(new PizzaModel(
"Margherita",
"Sauce tomate, Mozarella",
"5.80€",
"https://cdn.pixabay.com/photo/2020/06/08/16/49/pizza-5275191_960_720.jpg"));
pizzaList.add(new PizzaModel(
"Savoyarde",
"Sauce tomate, Mozarella, Pomme de terre, Fromage",
"10.20€",
"https://cdn.pixabay.com/photo/2017/11/23/16/57/pizza-2973200_960_720.jpg"));
pizzaList.add(new PizzaModel(
"Poulet pesto",
"Sauce tomate, Mozarella, Poulet, Pesto",
"8.50€",
"https://cdn.pixabay.com/photo/2016/06/08/00/03/pizza-1442945_960_720.jpg"));
pizzaList.add(new PizzaModel(
"4 Saisons",
"Sauce tomate, Mozarella, Olive, Basilic, Jambon, Champignons, Artichaut",
"9.60€",
"https://cdn.pixabay.com/photo/2016/06/08/00/03/pizza-1442946_960_720.jpg"));
recyclerViewPizzas = findViewById(R.id.recyclerViewPizzas);
recyclerViewPizzas.setHasFixedSize(true);
recyclerViewPizzas.setLayoutManager(new LinearLayoutManager(this));
pizzaAdapter = new PizzaAdapter(MainActivity.this, pizzaList);
recyclerViewPizzas.setAdapter(pizzaAdapter);
recyclerViewPizzas.addItemDecoration(new PizzaItemDecoration());
}
}
Adapter class :
package fr.balizok.pizzaapplication.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
import fr.balizok.pizzaapplication.PizzaModel;
import fr.balizok.pizzaapplication.R;
public class PizzaAdapter extends RecyclerView.Adapter<PizzaAdapter.ViewHolder>{
Context context;
List<PizzaModel> pizzaList;
public PizzaAdapter(Context context, List<PizzaModel> pizzaList) {
this.context = context;
this.pizzaList = pizzaList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.menu_pizza_design,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PizzaAdapter.ViewHolder holder, int position) {
PizzaModel currentPizza = pizzaList.get(position);
holder.pizza_name.setText(currentPizza.getName());
holder.ingredients.setText(currentPizza.getIngredients());
holder.price.setText(currentPizza.getPrice());
Glide.with(context).load(currentPizza.getImageURL()).into(holder.pizza_image);
}
#Override
public int getItemCount() {
return pizzaList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView pizza_image;
TextView pizza_name, ingredients, price;
public ViewHolder(#NonNull View itemView) {
super(itemView);
pizza_image = itemView.findViewById(R.id.pizza_image);
pizza_name = itemView.findViewById(R.id.pizza_name);
ingredients = itemView.findViewById(R.id.ingredients);
price = itemView.findViewById(R.id.price);
}
}
}
Hope you can help me, thank you in advance !
I follow your code and make it demo its resolved by using Picasso library.
Try this one I hope it would be helpful to you.
Picasso.get()
.load(currentPizza.getImageURL())
.resize(600, 200) // resizes the image to these dimensions (in pixel) if you want
.centerInside()
.into(holder.pizza_image);
Related
I'm trying to use a viewpager2 to show pictures taken from my app. Right now, I'm just using photos currently on my device to test, although the tutorial I followed (https://www.geeksforgeeks.org/viewpager2-in-android-with-example/) showed me how to make a viewpager2 with premade resource files. I'm trying to use an image bitmap instead, although whenever I run the code, the images come out blank. Is it an error with my files or is there a problem in my code? Here is my code:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
class ViewPager2Adapter extends RecyclerView.Adapter<ViewPager2Adapter.ViewHolder> {
// Array of images
// Adding images from drawable folder
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/DCIM/yes/yes_20220809_095311.jpg");
Bitmap bitmap2 = BitmapFactory.decodeFile("/sdcard/DCIM/yes/yes_20220809_095311___WITHTEXT.jpg");
private Bitmap[] images = {bitmap, bitmap2};
private Context ctx;
// Constructor of our ViewPager2Adapter class
ViewPager2Adapter(Context ctx) {
this.ctx = ctx;
}
// This method returns our layout
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ctx).inflate(R.layout.images_holder, parent, false);
return new ViewHolder(view);
}
// This method binds the screen with the view
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// This will set the images in imageview
holder.images.setImageBitmap(images[position]);
}
// This Method returns the size of the Array
#Override
public int getItemCount() {
return images.length;
}
// The ViewHolder class holds the view
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
images = itemView.findViewById(R.id.images);
}
}
}
PS: I am currently using hard coded files for testing, this will be changed later. The problem might be that the file path is wrong, but that is why I am asking this question.
I am trying to use the binding method in android studio to connect two fragments using onCreate and onViewCreated methods. so far i am getting id not resolved error. I have already connected the fragments on the xml graph. Bellow is the code of the settings java file.
package com.mqtt.workactiv.ui.settings;
import static android.os.Build.VERSION_CODES.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.mqtt.workactiv.databinding.FragmentSettingsBinding;
public class SettingsFragment extends Fragment {
private FragmentSettingsBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
SettingsViewModel settingsViewModel =
new ViewModelProvider(this).get(SettingsViewModel.class);
binding = FragmentSettingsBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
#Override
public void onViewCreated (#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController = Navigation.findNavController(view);
Button gateConnButton;
gateConnButton = view.findViewById(R.id.gatewayConnButton);
gateConnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
navController.navigate(R.id.action_navigation_setting_to_gateway4);
}
});
}
}
Make sure to add this in build.gradle(app)
buildFeatures {
viewBinding = true
}
Then give sync project with gradle files.
If this didnt work, try rebuild, invalidate cache/ restart.
Also view.findviewbyid not required if view binding is enabled.
You can access button directly by something like this,
binding.gateConnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
navController.navigate(R.id.action_navigation_setting_to_gateway4);
}
});
You can try code like this.
NavDirections navDirections = SettingsFragmentDirections.actionNavigationSettingToGateway4();
findNavController().navigate(navDirections, null);
I don't see your navigation xml file, so SettingsFragmentDirections.actionNavigationSettingToGateway4() can have another name. Fix those names yourself.
P.S. the versions of libraries which I'm using androidx.navigation:navigation-fragment-ktx:2.4.0, "androidx.navigation:navigation-ui-ktx:2.4.0"
I am making 50 data appear in my code, but I am putting it in a recycler view in a fragment because I have a dropdown menu, but when I put the code to call everything I get these two lines of error:
package com.example.tallerof.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tallerof.R;
import com.example.tallerof.adapters.UsuariosAdapter;
import java.util.ArrayList;
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
ArrayList<String> ListadeDatos;
RecyclerView recyclerView;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
recyclerView=(RecyclerView) findViewById(R.id.Recycler1); /* Error #1 Cannot resolve method findViewById in GalleryFragment*/
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); /* Error #2 required type: context provide: Gallery Fragment*/
ListadeDatos=new ArrayList<String>();
for(int i=0; i<=50; i++){
ListadeDatos.add("Dato # "+i+"");
}
UsuariosAdapter adapter= new UsuariosAdapter(ListadeDatos);
recyclerView.setAdapter(adapter);
return null;
}
}
you must to do this.
recyclerView = getActivity().findViewbyId(R.id.Recycler1);
Regards.
I'm creating a Material design tab view. I'm getting an error after addFragment method on my Main Activity. This is the error that I'm getting.
Wrong 1st argument type. Found: 'com.example.sa.tabproject.LatestPromoFragment, ' android.supportv4.app.Fragment'
My MainActivity.java
import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.app.Fragment;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
tabLayout=(TabLayout)findViewById(R.id.tab_layout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter= new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(new LatestPromoFragment(),"Latest Promos"); // new LatestPromoFragment() should be in getItem() otherwise it will cause crashes
viewPagerAdapter.addFragment(new MapViewFragment(),"Map View"); // new MapViewFragment() should be in getItem() otherwise it will cause crashes
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}}
ViewPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragment = new ArrayList<>(); // this line can cause crashes
ArrayList<String> tabTitles= new ArrayList<>();
public void addFragment(Fragment fragment,String titles){
this.fragment.add(fragment); // this line can cause crashes
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragment.get(position); // this should create the Fragment instances
}
#Override
public int getCount() {
return fragment.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}}
I imported several fragment classes to the MainActivity.java. But it doesn't fix the error.
This is because in adapter class you are using support fragment i.e import android.support.v4.app.Fragment; and in Activity class you are using app fragment import android.app.Fragment;
use same type of fragment every where it will work.
If you use android.support.v4.app.FragmentPagerAdapter, you must also use android.support.v4.app.Fragment for all Fragments.
If you want to use android.app.Fragment native Fragments with a FragmentPagerAdapter or FragmentStatePagerAdapter, you need to use the v13 support library versions of the adapter, i.e. android.support.v13.app.FragmentPagerAdapter.
You can see this in the documentation if you look at the getItem() for each:
V4:
http://developer.android.com/intl/en/reference/android/support/v4/app/FragmentPagerAdapter.html#getItem(int)
V13:
http://developer.android.com/intl/en/reference/android/support/v13/app/FragmentPagerAdapter.html#getItem(int)
Replace your import android.app.Fragment; code
with
import android.support.v4.app.Fragment;
So I have this class here:
package phil.droid.game;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class GameList extends GamesTrialActivity
{
private ListView lv1;
protected String Game_names[]={"God of War","FOS RO DAH", "dhwaud"};
private String Game_pics[]={"God of War","God of War II"};
private int pos;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gamelist);
lv1=(ListView)findViewById(R.id.thegamelist);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , Game_names));
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
pos = position;
Intent i = new Intent(GameList.this, game_viewer.class);
startActivity(i);
}
});
}
}
And then this VclassV extends the one ^above^
package phil.droid.game;
import android.os.Bundle;
import android.widget.TextView;
public class game_viewer extends GameList
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game_template);
TextView game_title = (TextView)findViewById(R.id.GameTitle);
game_title.setText("" + pos);
}
}
The problem is at the moment that the last bit recognizes "pos" as "0" no matter what option I click on. Can someone suggest a way to make it so pos is recognized as the number element that's clicked on in the previous class?
Make pos protected, not private.
What you're trying to do can't work: The newly launched activity will not share the same storage as the parent, even if they inherit. The only way it would be possible is if the value was static, but that's not a good idea either.
What you should do instead is to send the data as part of the intent before starting the activity, e.g.:
intent.putExtra("pos", position);
and then you can pull it out in the new activity with
getIntent().getIntExtra("pos", -1); // -1 is used as default value
Also, game_viewer should most likely be a separate activity, rather than inherit from GameList.