Store state for each item separately in RecyclerView - android

RecyclerView OnClick doesn't work properly. It has to change state to true or false. When I click on the first item it stores true and changes the text. But when I click on other item it changes the state to false because the first item set true, so the text doesn't change. How to store the states for each item separately?
boolean isChange = false;
#Override
public void onBindViewHolder(#NonNull final WindowViewHolder holder, int position) {
Window window = windowList.get(position);
holder.textViewTitle.setText(window.getTitle());
holder.textViewChecked.setText(window.getCheck());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(window.getImage()));
////////////////////////////////////////////////
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isChange){
holder.parentLayout.setBackgroundColor(Color.parseColor("#66BB6A"));
holder.textViewChecked.setText("True");
isChange = true;
}
else {
holder.parentLayout.setBackgroundColor(Color.parseColor("#EEEEEE"));
holder.textViewChecked.setText("False");
isChange = false;
}
//Toast.makeText(mCtx, "Clicked", Toast.LENGTH_SHORT).show();
}
});
//////////////////////////////////////
}

I hope you have a setCheck() method for your Window object where you store the checked state. The isChange variable you use is not item specific so it keeps the value of the previous checked item.
From what I understand by your code you should do this:
#Override
public void onBindViewHolder(#NonNull final WindowViewHolder holder, int position) {
Window window = windowList.get(position);
holder.textViewTitle.setText(window.getTitle());
holder.textViewChecked.setText(window.getCheck());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(window.getImage()));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (window.getCheck().toString.equals("False")) {
window.setCheck("True");
holder.parentLayout.setBackgroundColor(Color.parseColor("#66BB6A"));
holder.textViewChecked.setText("True");
} else {
window.setCheck("False");
holder.parentLayout.setBackgroundColor(Color.parseColor("#EEEEEE"));
holder.textViewChecked.setText("False");
}
}
});
}

Why not to use Map?
I see you have some Window model. So you can create a map in you adapter and store a boolean value for each Window key:
private Map<Window, Boolean> map = new HashMap<>();
And then get value for the current value:
#Override
public void onBindViewHolder(#NonNull final WindowViewHolder holder, int position) {
Window window = windowList.get(position);
holder.textViewTitle.setText(window.getTitle());
holder.textViewChecked.setText(window.getCheck());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(window.getImage()));
////////////////////////////////////////////////
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isChange = map.get(window);
if (!isChange){
holder.parentLayout.setBackgroundColor(Color.parseColor("#66BB6A"));
holder.textViewChecked.setText("True");
map.put(window, true);
}
else {
holder.parentLayout.setBackgroundColor(Color.parseColor("#EEEEEE"));
holder.textViewChecked.setText("False");
map.put(window, false);
}
//Toast.makeText(mCtx, "Clicked", Toast.LENGTH_SHORT).show();
}
});
//////////////////////////////////////
}

Related

Checked the Checkboxes that is already checked

I have one EditText (edModelColor). when user click on edModelColor(EditText) then the custom dialog will be called.Custom dialog consist of RecylerView and searchview and one custom row for each item. Custom row contains ImageView(icon), TextView (colorNames) and checkbox for selection. When user click on any checkbox I passed the colorName and its position in adapter into method checkAndRemove. this method will add or remove the color name according to its adapter position and the colorNames will added into edModelColor(EditText). its working fine but the problem is that once the user click on edModelColor(EditText) again, I want to checked those checkboxes which are already checked inside CustomDialogbox.I have seen some articles online but I could not understand what they meant.
bodyColorDialog:
private void bodyColorDialog() {
TextView txtClose;
TextView tvCancel;
Button btnOk;
bodyColorDialog.setContentView(R.layout.ed_body_color_dialog);
bodyColorDialog.setCancelable(false);
txtClose = bodyColorDialog.findViewById(R.id.txtModelClose);
tvCancel = bodyColorDialog.findViewById(R.id.tvCancel);
btnOk = bodyColorDialog.findViewById(R.id.btnOk);
edBodyColorSearchView = bodyColorDialog.findViewById(R.id.edBodyColorSearchViewColor);
edBodyColorRecylerView = bodyColorDialog.findViewById(R.id.edBodyColorRecylerView);
edBodyColorRecylerView.setLayoutManager(new LinearLayoutManager(getContext()));
bodyColorArrayList.clear();
setUpBodyColorArrayList();
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectionCMap = new HashMap<>();
selectionCMap = edBodyColorAdapter.selectionColorsMap;
for(String value : selectionCMap.values()){
/* tv.setText(tv.getText() + "\n" + value);*/
edBodyColor.append(value + ",");
}
bodyColorDialog.dismiss();
}
});
txtClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bodyColorDialog.dismiss();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bodyColorDialog.dismiss();
}
});
bodyColorDialog.show();
}
private void setUpBodyColorArrayList() {
bodyColorArrayList.clear();
String bodyColorName[] = getResources().getStringArray(R.array.body_color_array);
int bodyColorIcons[] = {R.drawable.red, R.drawable.black, R.drawable.violet, R.drawable.white,
R.drawable.orange, R.drawable.blue, R.drawable.green, R.drawable.yello};
for(int i =0; i < bodyColorIcons.length; i++)
{
bodyColorArrayList.add(new edModelBodyColor(bodyColorName[i], bodyColorIcons[i]));
}
edBodyColorAdapter = new edBodyColorAdapter(getContext(), bodyColorArrayList);
edBodyColorRecylerView.setAdapter(edBodyColorAdapter);
edBodyColorSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String queryString) {
edBodyColorAdapter.getFilter().filter(queryString);
return false;
}
#Override
public boolean onQueryTextChange(String queryString) {
edBodyColorAdapter.getFilter().filter(queryString);
return false;
}
});
}
edBodyColorAdapter.java
holder.checkBoxColor.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
int position = holder.getAdapterPosition();
clickedColorNamePosition = edBodyColorArrayList.indexOf(filteredArrayList.get(position));
String name = edBodyColorArrayList.get(clickedColorNamePosition).getBodyColorName();
Toast.makeText(context, "name = " + name, Toast.LENGTH_SHORT).show();
//this mthod will check if selected checkbox value is already present or not.
// It present then remove ( means user unchecked box) and if value is not there means user has selected checkbox
checkAndRemove(position,name);
}
});
checkAndRemove:
private void checkAndRemove(int position, String name) {
if(selectionColorsMap.containsKey(position)){
selectionColorsMap.remove(position);
Toast.makeText(context, "removed", Toast.LENGTH_SHORT).show();
}else {
selectionColorsMap.put(position, name);
Toast.makeText(context, "added", Toast.LENGTH_SHORT).show();
}
}
preview:
Conculusion: I want to check these checkboxes values when user click again on edBodyColor dialog..
I as can see, you are initializing the ArrayList of colors every time you are showing the dialog, that's why, you start always at initial state.
Instead, you should initialized the ArrayList inside onCreate() and reuse it every time to maintain the previous state.

After clicking a button view is updating on both items of recyclerview

I have a RecyclerView with post cards. So there is a like button in every post card. When I tap on a like button. Sometimes it works fine and updates on current item. But after some scroll when I tap on like button it updates on another item simultaneously.
It doesn't send that data to server. But, only the view is updated. Then when I scroll up and down again. The data goes back to normal.
Problem is in the following onClick method:-
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("like").exists()){
holder.like.setImageResource(R.drawable.ic_favorite_red_500_36dp);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").removeValue();
}
});
}else if(dataSnapshot.child("dislike").exists()) {
holder.like.setImageResource(R.drawable.brokenheart);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").removeValue();
}
});
}else{
holder.like.setImageResource(R.drawable.ic_favorite_border_red_500_36dp);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").removeValue();
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").setValue(true);
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
holder.like.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").removeValue();
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").setValue(true);
return true;
}
});
Full Adapter class https://pastebin.com/UnFGahWT
Try the following:-
Set Your on click listeners in the public ViewHolder(View itemView) method and make Your ViewHolder class implement the View.OnClickListener.
In Adapter add:
public Topic getItem(int position) {
return topics.get(position);
}
In ViewHolder's onClick method add:
int position = getAdapterPosition();
if (position == RecyclerView.NO_POSITION) return;
item = getItem(position);
Thus You will get the exact object You need to change or do something with it.

RecyclerView index not updating while removing item using FirebaseRecycleradapter

I have implemented FirebaseRecyclerAdapter for a dynamic recyclerView and while I am trying to delete an item from the list, initially items at wrong indexes are getting deleted and after more items deletion, i am getting IndexOutOfBoundsException. I have already tried all solutions I found which can be seen as commented inside populateViewHolder() method. Please, if you have to downvote, at least give a proper answer and downvote it as I have already tried all solutions possible. Below I am posting my code. Please have a look.
Check the delete.setOnClickListener():
adapter = new FirebaseRecyclerAdapter<Slc, viewHolder>(
Slc.class,
R.layout.single_slc_row,
viewHolder.class,
FirebaseDatabase.getInstance().getReference().child("DCU")) {
#Override
protected void populateViewHolder(final viewHolder viewHolder, final Slc model, final int position)
{
//expand collapse
viewHolder.vis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// ViewAnimationsUtils.expand(viewHolder.inv);
viewHolder.collapse.setVisibility(View.VISIBLE);
viewHolder.inv.setVisibility(View.VISIBLE);
}
});
viewHolder.collapse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// ViewAnimationsUtils.collapse(inv);
viewHolder.inv.setVisibility(View.GONE);
viewHolder.collapse.setVisibility(View.GONE);
}
});
String s=adapter.getRef(position).getKey();
viewHolder.name.setText(s);
//dimming get
viewHolder.dimming.setProgress(model.getPercentage());
//on off get
if(model.getON_OFF()==1)
{
viewHolder.onOff.setChecked(true);
}
else if(model.getON_OFF()==0)
{
viewHolder.onOff.setChecked(false);
}
//dimming set
//viewHolder.dimming.incrementProgressBy(10);
viewHolder.showProgress.setText( viewHolder.dimming.getProgress()+"%");
viewHolder.dimming.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if(position!=RecyclerView.NO_POSITION)
{
if(seekBar.isShown() && fromUser)
{
progress= (progress/10)*10;
adapter.getRef(position).child("Percentage").setValue(progress);
viewHolder.showProgress.setText(progress+"%");
}
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
viewHolder.onOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(buttonView.isPressed() &&isChecked){
adapter.getRef(position).child("ON_OFF").setValue(1);
}else{
adapter.getRef(position).child("ON_OFF").setValue(0);
}
}
});
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
final Dialog dialog = new Dialog(myContext);
dialog.setTitle("Delete Slc?");
dialog.setContentView(R.layout.delete_slc);
dialog.setCancelable(false);
Window window = dialog.getWindow();
if(window == null) return;
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
TextView delete = (TextView)dialog.findViewById(R.id.deleteSlc);
final String key= adapter.getRef(position).getKey();
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// int pos = viewHolder.getAdapterPosition();
ref.child(key).setValue(null);
// int selectedItems = position;
// for (int i = selectedItems; i >= selectedItems; i--) {
// adapter.getRef(i).removeValue();
adapter.notifyItemRemoved(position);
// }
// adapter.getRef(position).removeValue();
//
// adapter.notifyItemRemoved(position);
// adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
TextView dialogButton = (TextView) dialog.findViewById(R.id.canceld);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
});
}
};
slcList.setAdapter(adapter);
The exception I am getting after deleting some items:
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.firebase.ui.database.FirebaseArray.getItem(FirebaseArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getRef(FirebaseRecyclerAdapter.java:150)
The error is caused by usage of position object inside of function not directly under populateViewHolder. Like in this one: (but not limited to)
protected void populateViewHolder(final viewHolder viewHolder, final Slc model, final int position) {
// here position object is valid
...
viewHolder.dimming.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged( ... ) {
// but here, position object is likely to be incorrect
// especially when you add/remove item of list
}
...
}
And the solution is simply to replace all the position objects that are not directly under populateViewHolder into viewHolder.getAdapterPosition().
Note: I copy it from my comment in case others seek for answer and miss the comment

How to keep selecting rows of RecyclerView during onClicks after pressing longClick?

I have a RecyclerView where I am selecting my rows only when I do onLongPress but what I want is that the first onLongClick will initiate the highlighting and then onClick on the row will select and de-select the particular rows and NOT onLongClick. After de-selecting the last selected row then onLongClick should initiate the highlighting again. I tried doing it with boolean variables. But it does not work. I am so confused. Any ideas?
This is what I have been upto.
if(onLongPressReceived) {
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!onClickAnotherPressReceived) {
onClickPressReceived = true;
mItems.get(holder.getAdapterPosition()).setmIsChecked(true);
notifyDataSetChanged();
onClickAnotherPressReceived = true;
}
}
});
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(mItems.get(position).getmIsChecked());
holder.mDeleteRow.setVisibility(View.VISIBLE);
if(mItems.get(holder.getAdapterPosition()).getmIsChecked()){
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_selected));
}else {
//holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.checkboxHolder.setVisibility(View.INVISIBLE);
holder.mDeleteRow.setVisibility(View.INVISIBLE);
}
if(onClickPressReceived && !onLongAnotherPressReceived)
{
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(mItems.get(position).getmIsChecked());
holder.mDeleteRow.setVisibility(View.VISIBLE);
if(mItems.get(holder.getAdapterPosition()).getmIsChecked()){
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_selected));
}else {
//holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.checkboxHolder.setVisibility(View.INVISIBLE);
holder.mDeleteRow.setVisibility(View.INVISIBLE);
}
}
//Checking whether a particular view is clicked or not
if(onClickAnotherPressReceived && !onLongAnotherPressReceived)
{
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_unselected));
holder.mDeleteRow.setVisibility(View.INVISIBLE);
if (mItems.get(position).getmIsChecked()) {
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(true);
} else {
holder.checkboxHolder.setVisibility(View.GONE);
holder.mCheckBox.setChecked(false);
}
}
}
//Checking whether a particular view is clicked or not
else{
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_unselected));
holder.mDeleteRow.setVisibility(View.INVISIBLE);
if (mItems.get(position).getmIsChecked()) {
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(true);
} else {
holder.checkboxHolder.setVisibility(View.GONE);
holder.mCheckBox.setChecked(false);
}
}
//Calls the interface method in Activity to respond to CheckBox changes
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
updateMainClass.updateListBackground(holder.getAdapterPosition(), b);
}
});
/**
* <p>Responds to long press made on any row</p>
* <p>Checks the item on which long press is made
* sets the onLongPressReceived status to true and notify the adapter to refresh the list.</p>
*/
holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
if(onLongAnotherPressReceived) {
onLongPressReceived = true;
mItems.get(holder.getAdapterPosition()).setmIsChecked(true);
notifyDataSetChanged();
onLongAnotherPressReceived = false;
}
return true;
}
});
//Calls the interface in Activity to remove the item from the List.
holder.mDeleteRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateMainClass.updateItemList(holder.getAdapterPosition());
}
});
}
I usually do this:
In your onBindViewHolder call bindView method of ViewHolder class:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
((ViewHolder) viewHolder).bindView(getItem(position));
}
Then in your viewHolder class do keep instance of your entity:
public class ViewHolder extends RecyclerView.ViewHolder {
YOUR_OBJECT entity;
public ViewHolder(View itemView) {
super(itemView);
//initialize view here
}
public void bindView(ChannelEntity entity) {
this.entity = entity; // use this entity for longClickListener
//setOnClickListener || OnLongClickListener Here
}
}

RecyclerView getAdapterPosition() returns -1 on a callback so I can't show the new appearance for the item

Each item on my RecyclerView has a button that has three states: OPEN, LOADING, and CLOSED.
Initially all the buttons are in the OPEN state. When a button is clicked, the state is changed to LOADING and a network call is performed in the background. After the network call succeeds, the button state should be changed to CLOSED.
So in my adapter I used the following:
holder.button.setOnClickListener(v -> {
holder.state = LOADING;
notifyItemChanged(holder.getAdapterPosition()); /* 1 */
callNetwork(..., () -> {
/* this is the callback that runs on the main thread */
holder.state = CLOSED;
notifyItemChanged(holder.getAdapterPosition()); /* 2 */
});
});
The LOADING state is always visualized correctly at /* 1 */ because getAdapterPosition() gives me the correct position.
However, the CLOSED state of the button is never visualized, because getAdapterPosition at /* 2 */ always returns -1.
I might understand getAdapterPosition() wrongly in this case.
How do I refresh the appearance of an item on a callback?
From the docs:
Note that if you've called notifyDataSetChanged(), until the next
layout pass, the return value of this method will be NO_POSITION
NO_POSITION is a constant whose value is -1. This might explain why you are getting a return value of -1 here.
In any case, why don't you find the position of the model in the underlying dataset and then call notifyItemChanged(int position)? You could save the model as a field in the holder.
For example:
public class MyHolder extends RecyclerView.ViewHolder {
private Model mMyModel;
public MyHolder(Model myModel) {
mMyModel = myModel;
}
public Model getMyModel() {
return mMyModel;
}
}
holder.button.setOnClickListener(v -> {
holder.state = LOADING;
notifyItemChanged(holder.getAdapterPosition());
callNetwork(..., () -> {
/* this is the callback that runs on the main thread */
holder.state = CLOSED;
int position = myList.indexOf(holder.getMyModel());
notifyItemChanged(position);
});
});
Alternatively you can just ignore if the position is -1, like this:
holder.button.setOnClickListener(v -> {
holder.state = LOADING;
int preNetworkCallPosition = holder.getAdapterPosition();
if (preNetworkCallPosition != RecyclerView.NO_POSITION) {
notifyItemChanged(preNetworkCallPosition);
}
callNetwork(..., () -> {
/* this is the callback that runs on the main thread */
holder.state = CLOSED;
int postNetworkCallPosition = holder.getAdapterPosition();
if (postNetworkCallPosition != RecyclerView.NO_POSITION) {
notifyItemChanged(postNetworkCallPosition);
}
});
});
getAdapterPosition(); It will always return -1 when recyclerview makes layout calculations. You are calling this methods inside ViewHolder.. It means RecyclerView is doing calculations.
If you need position inside click actions of view, call it in the public void onClick(final View v) method for example:
"#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Students user = mUsers.get(position);
holder.Name.setText(user.getFullname());
holder.Index.setText(user.getIndex_number());
if (user.getThumbnail().equals("default")) {
holder.profile_image.setImageResource(R.drawable.profile_pic);
} else {
Picasso.get().load(user.getThumbnail())
.placeholder(R.drawable.profile_pic)
.into(holder.profile_image);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
**list_user_id = mUsers.get(position).getId();**
Intent Sub = new Intent(mContext, UserProfileActivity.class);
Sub.putExtra("user_id1", list_user_id);
mContext.startActivity(Sub);
BUT NOT
getAdapterPosition(); It will always return -1 when recyclerview makes layout calculations. You are calling this methods inside ViewHolder.. It means RecyclerView is doing calculations.
If you need position inside click actions of view, call it in the public void onClick(final View v) method for example:
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Students user = mUsers.get(position);
holder.Name.setText(user.getFullname());
holder.Index.setText(user.getIndex_number());
**list_user_id = mUsers.get(position).getId();**
if (user.getThumbnail().equals("default")) {
holder.profile_image.setImageResource(R.drawable.profile_pic);
} else {
Picasso.get().load(user.getThumbnail())
.placeholder(R.drawable.profile_pic)
.into(holder.profile_image);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
Intent Sub = new Intent(mContext, UserProfileActivity.class);
Sub.putExtra("user_id1", list_user_id);
mContext.startActivity(Sub);

Categories

Resources