Jaydeep Dhamecha Jaydeep Dhamecha - 1 month ago 10
Android Question

How to pass id instead of position using recyclerView in android

I have pass position in recyclerView click but some kind of problem pass to position to display wrong data so how can i pass people id in recyclerView.

I m new in android programming

recyclerView Item Click

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
buildCustomAdapter = new BuildCustomAdapter(this, peopleList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
buildCustomAdapter.notifyDataSetChanged();
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(buildCustomAdapter);
buildCustomAdapter.setOnItemClickListener(new BuildCustomAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
detailPeople(position);
}
});

private void detailPeople(int position) {
Intent intent = new Intent(this, AddDetail.class);
intent.putExtra("peopleID", position);
startActivity(intent);
}


Model.class

public class People implements Serializable {
private String peopleImage;
private String peopleName;
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public void setPeopleName(String peopleName) {
this.peopleName = peopleName;
}

public String getPeopleName() {
return peopleName;
}

public void setPeopleImage(String peopleImage) {
this.peopleImage = peopleImage;
}

public String getPeopleImage() {
return peopleImage;
}
}


Adapter code

public class BuildCustomAdapter extends RecyclerView.Adapter<BuildCustomAdapter.MyViewHolder> implements Filterable {
private List<People> peopleList;
private List<People> peopleListCopy;
private ItemFilter mFilter = new ItemFilter();
private OnItemClickListener mOnItemClickListener;
private Context mContext;

public BuildCustomAdapter(Context context, List<People> buildList) {
mContext = context;
this.peopleList = buildList;
this.peopleListCopy = new ArrayList<>();
peopleListCopy.addAll(buildList);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.build_list_row, parent, false);

return new MyViewHolder(itemView);
}


@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

People people = peopleList.get(position);

byte[] decodedString = Base64.decode(people.getPeopleImage(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.ivPeopleImage.setImageBitmap(decodedByte);
holder.tvPersonName.setText(people.getPeopleName());

holder.button.setSelected(people.getStatus() == 1);
holder.button.setOnClickListener(new onSelectListener(position));

}

@Override
public int getItemCount() {
return peopleList.size();
}

@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView tvPersonName;
public Button button;
public CircularImageView ivPeopleImage;


public MyViewHolder(View itemView) {
super(itemView);
mContext = itemView.getContext();
ivPeopleImage = (CircularImageView) itemView.findViewById(R.id.ivPerson);
tvPersonName = (TextView) itemView.findViewById(R.id.tvPersonName);
button = (Button) itemView.findViewById(R.id.addbn);
tvPersonName.setOnClickListener(this);
ivPeopleImage.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (mOnItemClickListener != null)
mOnItemClickListener.onItemClick(v, getPosition());
}
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}

public interface OnItemClickListener {
void onItemClick(View view, int position);

}

private class ItemFilter extends Filter {

protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<People> filterList = new ArrayList<>();

for (int i = 0; i < peopleListCopy.size(); i++) {
if ((peopleListCopy.get(i).getPeopleName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {

People peopleName = peopleListCopy.get(i);
filterList.add(peopleName);
}
}
results.count = filterList.size();
results.values = filterList;

} else {
results.count = peopleListCopy.size();
results.values = peopleListCopy;
}
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
peopleList = (List<People>) results.values;
notifyDataSetChanged();
}
}


private class onSelectListener implements View.OnClickListener {

int mPosition;

public onSelectListener(int position) {
mPosition = position;
}

@Override
public void onClick(View view) {
People people = peopleList.get(mPosition);
view.setSelected(!view.isSelected());
people.setStatus(view.isSelected() ? 1 : 0);
notifyDataSetChanged();
}
}


}
Next Activity to get intent

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int peopleID = bundle.getInt("peopleID");
peopleList.clear();

BuildDataa();
People peopleDetailsObj = peopleList.get(peopleID);
}

Answer

Please replace

@Override
public void onClick(View v) {
    if (mOnItemClickListener != null)
        mOnItemClickListener.onItemClick(v, getPosition());
}

to

    @Override
public void onClick(View v) {
    if (mOnItemClickListener != null)
        mOnItemClickListener.onItemClick(v, peopleList.get(getPosition()).getId());
}

in your adapter. And change signature of method onItemClick from

onItemClick(View v, int position)

to

onItemClick(View v, String id)

and use this to get People according to ID

  People peopleDetailsObj=null;
  for(People ple:peopleList)
  {
    if(ple.getId().equals(peopleID)){
  peopleDetailsObj=ple;
     break;
   }}