Reputation: 1315
I have a small problem, I am trying to display a list of items, but it is not working, each item contains an image, so here is my Activity :
public class TestRecyclerImagesActivity extends AppCompatActivity {
@BindView(R.id.listImages)
RecyclerView listImages;
RecyclerView.Adapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.images_list);
ButterKnife.bind(this);
ImageModel img = new ImageModel("test1");
List list = new ArrayList<ImageModel>();
list.add(img);
list.add(img);
list.add(img);
list.add(img);
adapter = new ImageListAdapter(this, list);
this.listImages.setAdapter(adapter);
this.listImages.setLayoutManager(new LinearLayoutManager(this));
}
}
And here is my Adapter :
public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ImageViewHolder>{
List<ImageModel> images;
Context context;
public ImageListAdapter(Context context, List<ImageModel> contracts){
this.context = context;
this.images = contracts;
}
public void updateImages(List<ImageModel> newImages){
images.clear();
images.addAll(newImages);
notifyDataSetChanged();
}
@NonNull
@Override
public ImageListAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image, parent, false); // ContratBinding >> as your list item layout named "contrat"
return new ImageListAdapter.ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ImageListAdapter.ImageViewHolder holder, int position) {
holder.bind(images.get(position));
}
@Override
public int getItemCount() {
return this.images.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.img)
ImageView image;
public ImageViewHolder(View iview){
super(iview);
ButterKnife.bind(this, iview);
}
void bind(ImageModel imageModel){
image.setImageResource(R.drawable.newimage);
}
}
}
This my listImages layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listImages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</LinearLayout>
This is my item :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" >
<ImageView
android:id="@+id/img"
android:src="@drawable/newimage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
So why it is not displaying anything? I feel like I did everything right.
For the Image src, it is not dynamic now, I am using directly an image in my drawable, I set the source with code, did not work, set it in the XML, did not work, set it in both and lol did not work.
Any help would be much appreciated.
Upvotes: 0
Views: 627
Reputation: 1128
First Thing Try setting layoutmanager before adapter
this.listImages.setLayoutManager(new LinearLayoutManager(this));
this.listImages.setAdapter(adapter);
Second thing
Make recyclerview width match_parent and its parent LinearLayout's width also match_parent
Third thing
Provide required orientation to your layout manager like vertical/horizontal
Fourth thing
Initialize ur arraylist in the adapter itself, and on update clear and addAll new list
public class ImageListAdapter extends RecyclerView.Adapter{
List<ImageModel> images = new ArrayList();
Context context;
public ImageListAdapter(Context context, List<ImageModel> contracts){
this.context = context;
this.images.clear();
this.addAll(contracts);
}
Fifth Thing
Try giving fixed height to the imageview, Currently it is wrap_content
Upvotes: 1
Reputation: 69
In the imageview you have wrap content try it in match_constrain, and you need on onCreateViewHolder to return your view and say that in the view like
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image, parent, false);
ImageView object = view.findViewById<ImageView>(R.id.img);
you have the image view now, now pick the image source and pass it to the source of the object.
Upvotes: 1