2

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);
}
Joy
  • 289
  • 1
  • 15

4 Answers4

2

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;
   }}
Brijesh Kumar
  • 1,685
  • 2
  • 17
  • 28
  • yaa pass the id i have get the intent in this line People peopleDetailsObj = peopleList.get(peopleID); peopleId is 0 how to solve it – Joy Oct 25 '16 at 06:36
  • You've to replace People peopleDetailsObj = peopleList.get(peopleID) with the above code – Brijesh Kumar Oct 25 '16 at 06:57
  • Bundle bundle = getIntent().getExtras(); if (bundle != null) { int peopleID = bundle.getInt("peopleID"); peopleList.clear(); BuildDataa(); People people =null; for (People ple : peopleList) { if (ple.getId().equals(peopleID)) { people = ple; break; } } – Joy Oct 25 '16 at 07:03
  • please change int peopleID = bundle.getInt("peopleID"); to String peopleID = bundle.getString("peopleID"); – Brijesh Kumar Oct 25 '16 at 07:06
  • sorry i forgot it it's my mistake done boss @brijesh kumar – Joy Oct 25 '16 at 07:09
  • plz cheak my another qe:-http://stackoverflow.com/questions/40123215/how-to-get-date-and-month-column-using-object-model-database-in-android – Joy Oct 25 '16 at 07:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126579/discussion-between-jaydeep-dhamecha-and-brijesh-kumar). – Joy Oct 25 '16 at 07:15
  • sry in this qu:-http://stackoverflow.com/questions/40211491/how-to-set-date-and-month-wise-notification-list-android – Joy Oct 25 '16 at 07:18
  • hello Brijesh kumar one prob in your example. – Joy Oct 27 '16 at 11:14
  • id and people id is match but after i search to people this time same position getting ? position to to pass id – Joy Oct 27 '16 at 11:45
  • suppose first time display 50 list okay this time match both id okay after i search i have getting 20 list after i click recyclerview this time wrong data getting.. – Joy Oct 27 '16 at 11:53
  • for(People ple:peopleList) { if(ple.getId().equals(peopleID)){ peopleDetailsObj=ple; break; }}People Object null error – Joy Oct 27 '16 at 12:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126831/discussion-between-brijesh-kumar-and-jaydeep-dhamecha). – Brijesh Kumar Oct 27 '16 at 12:04
0

Take the id from position.

String id=peopleList.get(position).getId();
detailPeople(id);

Pass that id in your method and you are done.

Avinash Verma
  • 2,572
  • 1
  • 18
  • 22
  • Add the clicklistner inside the adapter. Make root layout id then on that layout id put click listner inside bindview. It will work...you can try....I use like this...only...!!! – Avinash Verma Oct 25 '16 at 06:40
0

Use position to get People object from ArrayList. Change detailPeople() method with below code.

private void detailPeople(int position) {

Intent intent = new Intent(this, AddDetail.class);
intent.putExtra("peopleID", peopleList.get(position).getId());
startActivity(intent);
}
Sanjeet
  • 2,385
  • 1
  • 13
  • 22
0
intent.putExtra("id", peopleList.get(position).getId());
Basi
  • 3,009
  • 23
  • 28