How to get string value from a recyclerview - java

i have a recyclerview for a movie and i need the movie name for using intent putextra. I Already make the click listener and got the adapter position, but but it only returns the array of the adapter like in the picture.
picture of output in logcat
how can i get the strings so i can use the intent putextra?
here is my recyclerview code
public class movieRecyclerViewAdapter extends RecyclerView.Adapter<movieRecyclerViewAdapter.myViewHolder> {
private static final String TAG = "movieRecyclerViewAdapter";
private onMovieClickListener movieClickListener;
private final ArrayList<movie> movies;
public movieRecyclerViewAdapter(ArrayList<movie> movieArrayList, onMovieClickListener onMovieClickListener){
this.movies = movieArrayList;
this.movieClickListener = onMovieClickListener;
Log.d("mainMenuAdmin", "Adapter List size : " + movieArrayList.size() );
}
#NonNull
#Override
public movieRecyclerViewAdapter.myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_movie, parent, false);
return new myViewHolder(view, movieClickListener);
}
#Override
public void onBindViewHolder(#NonNull movieRecyclerViewAdapter.myViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
String img = movies.get(position).getMovie_image();
holder.judul.setText(movies.get(position).getNamaMovie());
holder.tahun.setText(movies.get(position).getTahunMovie());
Picasso.get().load(img).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher_round).into(holder.image);
}
#Override
public int getItemCount() {
return movies.size();
}
public static class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private final TextView judul, tahun;
private final ImageView image;
RelativeLayout parent;
onMovieClickListener onMovieClickListener;
public myViewHolder(#NonNull View itemView, onMovieClickListener onMovieClickListener) {
super(itemView);
judul = itemView.findViewById(R.id.txtJudul);
tahun = itemView.findViewById(R.id.txtTahun);
image = itemView.findViewById(R.id.imagePoster);
parent = itemView.findViewById(R.id.parent_layout);
this.onMovieClickListener = onMovieClickListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onMovieClickListener.onMovieClick(view, this.getLayoutPosition());
}
}
public interface onMovieClickListener{
void onMovieClick(View v, int position);
}}
i've tried everything but it still return the array value not the string value of movie name

You have made a good effort 👌 but you are doing few things wrong here. Let me explain you all things step by step.
You need a string value from your click listener but you are passing a view + position in click. so to fix this you firstly need to change you interface like this.
public interface onMovieClickListener{
void onMovieClick(movie movie, int position);
}
2nd mistake is that you don't have your movie in your ViewHolder. Thats why you can not pass string in your listener. To fix this you need to change your myViewHolder like below.
public static class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private final TextView judul, tahun;
private final ImageView image;
RelativeLayout parent;
movie movie;
onMovieClickListener onMovieClickListener;
public myViewHolder(#NonNull View itemView) {
super(itemView);
judul = itemView.findViewById(R.id.txtJudul);
tahun = itemView.findViewById(R.id.txtTahun);
image = itemView.findViewById(R.id.imagePoster);
parent = itemView.findViewById(R.id.parent_layout);
itemView.setOnClickListener(this);
}
public void bind(movie movie, onMovieClickListener onMovieClickListener)
{
this.movie = movie;
this.onMovieClickListener = onMovieClickListener;
}
#Override
public void onClick(View view) {
onMovieClickListener.onMovieClick(movie, this.getLayoutPosition());
}
}
Change your adapter functions like this.
#NonNull
#Override
public movieRecyclerViewAdapter.myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_movie, parent, false);
return new myViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull movieRecyclerViewAdapter.myViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
String img = movies.get(position).getMovie_image();
holder.judul.setText(movies.get(position).getNamaMovie());
holder.tahun.setText(movies.get(position).getTahunMovie());
Picasso.get().load(img).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher_round).into(holder.image);
holder.bind(movies.get(position), this.movieClickListener);
}
That's IT!!!
Now you have your movie in your listener and you can use your movie to get any variable inside movie like movie name etc..
I am pasting whole java file here so that you can just copy and paste code to test.
public class movieRecyclerViewAdapter extends RecyclerView.Adapter<movieRecyclerViewAdapter.myViewHolder> {
private static final String TAG = "movieRecyclerViewAdapter";
private onMovieClickListener movieClickListener;
private final ArrayList<movie> movies;
public movieRecyclerViewAdapter(ArrayList<movie> movieArrayList, onMovieClickListener onMovieClickListener){
this.movies = movieArrayList;
this.movieClickListener = onMovieClickListener;
Log.d("mainMenuAdmin", "Adapter List size : " + movieArrayList.size() );
}
#NonNull
#Override
public movieRecyclerViewAdapter.myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_movie, parent, false);
return new myViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull movieRecyclerViewAdapter.myViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
String img = movies.get(position).getMovie_image();
holder.judul.setText(movies.get(position).getNamaMovie());
holder.tahun.setText(movies.get(position).getTahunMovie());
Picasso.get().load(img).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher_round).into(holder.image);
holder.bind(movies.get(position), this.movieClickListener);
}
#Override
public int getItemCount() {
return movies.size();
}
public static class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private final TextView judul, tahun;
private final ImageView image;
RelativeLayout parent;
movie movie;
onMovieClickListener onMovieClickListener;
public myViewHolder(#NonNull View itemView) {
super(itemView);
judul = itemView.findViewById(R.id.txtJudul);
tahun = itemView.findViewById(R.id.txtTahun);
image = itemView.findViewById(R.id.imagePoster);
parent = itemView.findViewById(R.id.parent_layout);
itemView.setOnClickListener(this);
}
public void bind(movie movie, onMovieClickListener onMovieClickListener)
{
this.movie = movie;
this.onMovieClickListener = onMovieClickListener;
}
#Override
public void onClick(View view) {
onMovieClickListener.onMovieClick(movie, this.getLayoutPosition());
}
}
public interface onMovieClickListener{
void onMovieClick(movie movie, int position);
}}
**
Few suggestions:
Don't send View via your listener to your activity/fragment
Your camel case for your variables.
Start class name with capital letter.
**
These few suggestions will make your code more readable.
Happy Coding 💚!!

Change interface listener to :
public interface onMovieClickListener {
void onMovieClick(View v, Movie movie);
}
Then define your setOnClickListener as :
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onMovieClickListener.onMovieClick(v, movies.get(getAdapterPosition()));
}
}
});
You'll be able to get the selected movie and display name as needed.

Related

Android Studio: Deleting Item from ChildRecyclerView

I'm currently building a schedule app where users can input information, and the app will organize the information in a list with each day of the week as the header with each corresponding activity under the day. I'm using a parent child recyclerview to do this (i.e. each day of the week is a part of the parent recyclerview and each activity in the schedule is part of the child recyclerview). Right now I'm trying to add a swipe to delete information in the child recyclerview. Does anyone know how I would go about doing this using the parent child recyclerview configuration, where I have two different adapters for each recyclerview?
Code for Parent RecyclerView:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {
ArrayList<DayHeader> weekList;
private Context mContext;
public MainRecyclerViewAdapter(Context mContext, ArrayList<DayHeader> weekList) {
this.mContext = mContext;
this.weekList = weekList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.dayofweekheader,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
DayHeader section = weekList.get(position);
String nameofday = section.getDayName();
ArrayList<medinfo> meditems = section.getmMedItems();
holder.weekdayname.setText(nameofday);
MedicationAdapter childrecyclerAdapter = new MedicationAdapter(mContext,meditems);
holder.childrecyclerview.setAdapter(childrecyclerAdapter);
}
#Override
public int getItemCount() {
return weekList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView weekdayname;
RecyclerView childrecyclerview;
public ViewHolder(#NonNull View itemView) {
super(itemView);
weekdayname = itemView.findViewById(R.id.weekdayheader);
childrecyclerview = itemView.findViewById(R.id.childrecyclerview);
}
}
}
Code for Child RecyclerView:
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MedicationViewHolder>{
private ArrayList<medinfo> mMedList;
private Context mContext;
public static class MedicationViewHolder extends RecyclerView.ViewHolder {
public TextView mMedname;
public TextView mTime;
public TextView mNumbPills;
public ImageView popup;
public MedicationViewHolder(#NonNull View itemView) {
super(itemView);
mMedname = itemView.findViewById(R.id.medname);
mTime = itemView.findViewById(R.id.time);
mNumbPills = itemView.findViewById(R.id.numberofpills);
popup = itemView.findViewById(R.id.menumore);
}
}
public MedicationAdapter(Context mContext, ArrayList<medinfo> medList) {
this.mContext = mContext;
this.mMedList = medList;
}
#NonNull
#Override
public MedicationViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,parent,false);
MedicationViewHolder mvh = new MedicationViewHolder(v);
return mvh;
}
#Override
public void onBindViewHolder(#NonNull MedicationViewHolder holder, int position) {
medinfo currentItem = mMedList.get(position);
String medName = "Medication: " + currentItem.getmMedName();
holder.mMedname.setText(medName);
String medTime = "Time: " + currentItem.getmTime();
holder.mTime.setText(medTime);
String numbPills = "# of Pills: " + currentItem.getmNumbPills();
holder.mNumbPills.setText(numbPills);
}
#Override
public int getItemCount() {
return mMedList.size();
}
public void removeItem(int position) {
mMedList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mMedList.size());
}
}
I also have this SwipeItem class:
public class SwipeItem extends ItemTouchHelper.SimpleCallback {
MedicationAdapter mMedAdapter;
SwipeItem(MedicationAdapter mMedAdapter){
super(0,ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
this.mMedAdapter = mMedAdapter;
}
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getBindingAdapterPosition();
this.mMedAdapter.removeItem(position);
}
}
So how can I implement the SwipeItem in my adapter codes to delete each child item?

Change RecyclerView Selected Item background

i have a list and i showed that in recycler view
some of items have blue background and other items have gray background
i want to edit selected item background (
The selected item means the item that has been clicked )
this is my adapter class
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
private Context context;
private List<User> users = new ArrayList<>();
public UserAdapter(List<User> users, Context context) {
this.users = users;
this.context = context;
}
#NonNull
#Override
public UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new UserViewHolder(LayoutInflater.from(context).inflate(R.layout.item_user, parent, false));
}
#Override
public void onBindViewHolder(#NonNull UserViewHolder holder, int position) {
holder.binUser(users.get(position), position);
}
#Override
public int getItemCount() {
return users.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
private RelativeLayout rlItemUser;
public UserViewHolder(#NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_itemUser_name);
rlItemUser = itemView.findViewById(R.id.itemUser_rootView);
}
public void binUser(User user, int position){
tvName.setText(user.getName());
if (user.getMode().equals("passenger")){
rlItemUser.setBackgroundColor(context.getResources().getColor(R.color.colorGray));
tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
} else if (user.getMode().equals("driver")){
rlItemUser.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
tvName.setTextColor(context.getResources().getColor(R.color.colorGray));
}
}
}
in the bindUser method:
rlItemUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rtlItemUser.setBackgroundColor(context.getResources().getColor(your color);
adapter.notifyItemChanged(position);
}
});

Android Recycler View problem in "onBindViewHolder" function "static method cannot be called from non-static method"

I am new to android and java. I am not able to call ViewHolder.setCategoryName(name); in the onBindViewHolder function. I know there are probably similar questions but nothing has worked for me yet. The compiler gives error "non-static functions cannot be called from a static context", I haven't used static keyword anywhere in my code.
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private List<CategoryModel> categoryModelList;
public CategoryAdapter(List<CategoryModel> categoryModelList) {
this.categoryModelList = categoryModelList;
}
#NonNull
#Override
public CategoryAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int position) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_item,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryAdapter.ViewHolder holder, int position) {
String icon = categoryModelList.get(position).getCategoryIconLink();
String name = categoryModelList.get(position).getCategoryName();
ViewHolder.setCategoryName(name);
}
#Override
public int getItemCount() {
return categoryModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView categoryIcon;
private TextView categoryName;
public ViewHolder(#NonNull View itemView) {
super(itemView);
categoryIcon = itemView.findViewById(R.id.category_icon);
categoryName = itemView.findViewById(R.id.category_name);
}
private void setCategoryIcon(){
}
private void setCategoryName(String name){
categoryName.setText(name);
}
}
}
The problem is that your method isn't static but you are trying to call from a static context:
private void setCategoryName
You would need to do:
private static void setCategoryName
However, for this type of action, you can just use the holder variable:
holder.bind(categoryModelList.get(position))
try this:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private List<CategoryModel> categoryModelList;
public CategoryAdapter(List<CategoryModel> categoryModelList) {
this.categoryModelList = categoryModelList;
}
#NonNull
#Override
public CategoryAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int position) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_item,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryAdapter.ViewHolder holder, int position) {
holder.bind(categoryModelList.get(position))
}
#Override
public int getItemCount() {
return categoryModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView categoryIcon;
private TextView categoryName;
public ViewHolder(#NonNull View itemView) {
super(itemView);
categoryIcon = itemView.findViewById(R.id.category_icon);
categoryName = itemView.findViewById(R.id.category_name);
}
public void bind(CategoryModel categoryModel){
categoryName.setText(categoryModel.getCategoryName());
}
}
}

OnClickListener for the Recycler View returns nothing

I'm a beginner and I'm trying to build a simple voice recorder app. I've already posted on the website concerning the access of external storage. This problem is now solved but I've an another one concerning my RecyclerView.
I've followed a tutorial online in order to detect which view is clicked and play the audio accordingly (I use my RecyclerView to display all the recording files just created). Here's the tutorial in question : https://www.youtube.com/watch?v=ZXoGG2XTjzU
The problem is, the 'OnNoteClick' does absolutely nothing. How can I fix this ?
I created an interface with a function 'OnNoteClick' attached to it. I implemented this function in mainactivity.java. Basically, I've followed the tutorial.
\\\ Adapter Code
private OnNoteListener mOnNoteListener;
...
public RecyclerViewAdapter(#NonNull ArrayList<String> text, Context context, OnNoteListener onNoteListener) {
Text = text;
this.context = context;
this.mOnNoteListener = onNoteListener;
}
...
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
ViewHolder holder = new ViewHolder(view, mOnNoteListener);
return holder;
}
...
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;
CardView ParentLayout;
OnNoteListener onNoteListener;
public ViewHolder(#NonNull View itemView, OnNoteListener onNoteListener) {
super(itemView);
textView = itemView.findViewById(R.id.cardtext);
ParentLayout = itemView.findViewById(R.id.cardview);
this.onNoteListener = onNoteListener;
ParentLayout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener{
void onNoteClick(int position);
}
}
...
\\\ MainActivityCode
public class FileAndDirectoryActivity extends AppCompatActivity implements RecyclerViewAdapter.OnNoteListener {
...
private void initRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.recyclerview);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(mNames, this, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
...
#Override
public void onNoteClick(int position) {
Toast.makeText(this, "Good News : I'm working", Toast.LENGTH_SHORT).show();
}
}
...
I expect the app to show a message reading "I'm working" or something like that but in reality, when i touch the cardview, nothing happens.
Your problem is in:
public void onBindViewHolder(#NonNull ViewHolder holder, int i) {
holder.textView.setText(Text.get(i));
holder.ParentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Click on this card !");
}
});
}
You set a new click listener on ParentLayout so it overwrites click listener that you set in constructor. Try it without and it should work:
public void onBindViewHolder(#NonNull ViewHolder holder, int i) {
holder.textView.setText(Text.get(i));
}
Change your Adapter to below code and also Make sure you are fetching correct id of cardView
private OnNoteListener mOnNoteListener;
...
public RecyclerViewAdapter(#NonNull ArrayList<String> text, Context context, OnNoteListener onNoteListener) {
Text = text;
this.context = context;
this.mOnNoteListener = onNoteListener;
}
...
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.cardview, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
...
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;
CardView ParentLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.cardtext);
ParentLayout = itemView.findViewById(R.id.cardview);
ParentLayout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
mOnNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener{
void onNoteClick(int position);
}
}
No need to pass listener on ViewHolder constructor, because you have mOnNoteListener as global varible.
Just create setonclicklistener of view in onBind method. And call interface method on that.
Like
mOnNoteListener.onNoteClick(pos);
pos integer as position got in onBind method
Here i given you clear explanation,
Create one common Interface, named as CustomItemClickListenere
public interface CustomItemClickListenere {
public void onItemClick(View v, int position); }
Change your adapter class like below,
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<AddSingleItemList> data;
private Context mContext;
private CustomItemClickListenere listener;
public RecyclerViewAdapter(Context mContext, ArrayList<AddSingleItemList> data, CustomItemClickListenere listener) {
this.data = data;
this.mContext = mContext;
this.listener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_list_single_item, parent, false);
final ViewHolder mViewHolder = new ViewHolder(mView);
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(v, mViewHolder.getPosition());
}
});
return mViewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemTitle.setText(Html.fromHtml(data.get(position).getTitle()));
}
#Override
public int getItemCount() {
return data.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView itemTitle;
public CardView cardView;
ViewHolder(View v) {
super(v);
itemTitle = (TextView) v.findViewById(R.id.person_name);
cardView = (CardView) v.findViewById(R.id.cv);
}
}
}
adapter's xml item_list_single_item :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="#+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Here i have created on Pojo class for setting static items on Recyclerview, named as AddSingleItemList :
public class AddSingleItemList {
private String title;
long id;
/**
*
* #param id
* #param title
*/
public AddSingleItemList(long id, String title) {
this.id = id;
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Update you RecyclerViewActivity like below :
public class RecyclerViewActivity extends AppCompatActivity {
RecyclerView itemsList;
RecyclerViewAdapter adapter;
ArrayList<AddSingleItemList> data = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
itemsList = (RecyclerView) findViewById(R.id.items_list);
LinearLayoutManager llm = new LinearLayoutManager(this);
itemsList.setLayoutManager(llm);
itemsList.setHasFixedSize(true);
//Add you items into the Recyclerview list
data.add(new AddSingleItemList(1, "First Item"));
data.add(new AddSingleItemList(2, "Second Item"));
adapter = new RecyclerViewAdapter(this, data, new CustomItemClickListenere() {
#Override
public void onItemClick(View v, int position) {
Log.d("RecyclerViewActivity", "clicked position:" + position);
Toast.makeText(RecyclerViewActivity.this, "Good News : I'm working", Toast.LENGTH_SHORT).show();
// do what ever you want to do with it
}
});
itemsList.setAdapter(adapter);
}
}
Finally you will get result as you raised in your question,

RecyclerView is duplicating the first three items on a list

Im fairly new to android and i am trying to use a RecyclerView to display content hosted on firebase, but when it comes up the first three items are duplicated.
I have tried a few solutions around but none seem to work, any help would be great!
DiscountRecyclerAdapter.java
public class DiscountRecyclerAdapter extends RecyclerView.Adapter<com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder> {
public List<Discounts> discountsList;
public Context context;
private ImageView discountImageView;
public DiscountRecyclerAdapter(List<Discounts> discountsList){
this.discountsList = discountsList;
}
#Override
public com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.discount_list_item, parent, false);
context = parent.getContext();
return new com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(final com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder holder, int position) {
holder.setIsRecyclable(false);
String discountName = discountsList.get(position).getDiscount_name();
holder.setDiscount_Name(discountName);
String discountDescription = discountsList.get(position).getDiscount_description();
holder.setDiscount_Description(discountDescription);
String discountValue = discountsList.get(position).getDiscount_value();
holder.setDiscount_Value(discountValue);
String image_url = discountsList.get(position).getDiscount_image();
String thumbUri = discountsList.get(position).getDiscount_image();
holder.setDiscount_Image(image_url, thumbUri);
}
#Override
public int getItemCount() {
if(discountsList != null) {
return discountsList.size();
} else {
return 0;
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private TextView discount_name;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDiscount_Name(String message){
discount_name = mView.findViewById(R.id.discount_name);
discount_name.setText(message);
}
public void setDiscount_Description(String message){
discount_name = mView.findViewById(R.id.discount_description);
discount_name.setText(message);
}
public void setDiscount_Value(String message){
discount_name = mView.findViewById(R.id.discount_value);
discount_name.setText(message);
}
public void setDiscount_Image(String downloadUri, String thumbUri){
discountImageView = mView.findViewById(R.id.discount_image);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.image_placeholder);
Glide.with(context).applyDefaultRequestOptions(requestOptions).load(downloadUri).thumbnail(
Glide.with(context).load(thumbUri)
).into(discountImageView);
}
}
}
I believe the problem is coming from the discount adapter but I cant say for sure. The RecyclerView is displayed on a fragment if that could be influencing it I can attach other pieces if they are necessary.
There is nothing wrong with your code.So maybe you should try to override those two methods in your adapter class
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}

Categories

Resources