简体   繁体   中英

Can't get the position for view inside RecylerView Adapter

My recycler view adapter is as follows

    @Override
        public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.contacts_list_item, viewGroup, false);
            return new ContactViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(ContactViewHolder viewHolder,  int position) {
            super.onBindViewHolder(viewHolder, position);
            viewHolder.removeContact.setTag(position);
            Contact contact = contacts.get(position);
            viewHolder.contactName.setText(contact.getContactName());
            viewHolder.contactNumber.setText(contact.getContactNumber());
            viewHolder.removeContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Integer taggedPosition = (Integer) v.getTag();
                Log.wtf("ad","Posirtion"+ Integer.toString(taggedPosition));
                contacts.remove(Integer.parseInt(Integer.toString(taggedPosition)));
                notifyItemRemoved(Integer.parseInt(Integer.toString(taggedPosition)));
                }
            });
        }



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


        public void removeItem(int pos) {
            contacts.remove(pos);
    //      notifyItemRemoved(pos);
        }

        public void add(Contact contact, int position) {
            contacts.add(position, contact);
            notifyItemInserted(position);
        }

and my view holder is

class ContactViewHolder extends RecyclerView.ViewHolder  {

    public TextView contactName,contactNumber;
    public ImageButton removeContact;

    public ContactViewHolder(View itemView) {
        super(itemView);
        contactName= (TextView) itemView.findViewById(R.id.tv_contactName);
        contactNumber= (TextView) itemView.findViewById(R.id.tv_contactNumber);
        removeContact= (ImageButton) itemView.findViewById(R.id.ib_removeItem);
    }
}

The issue is i cant seem to get the postion of viewHolder.removeContact .All i want to achieve is to remove the row when the viewHolder.removeContact is clicked.I have tried getting the postion by getTag() method also but it gives me only one postion ie 0.

Thanks to @pskink.Thanks. Solved the issue all i did was to pass the reference of my adapter and contacts arraylist to my view holder class like this

@Override
    public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.contacts_list_item, viewGroup, false);
        return new ContactViewHolder(itemView,this,contacts);
    }

where itemView is the View to be returned, this is the adapter refrence and contacts is the arraylist i have to updated on button click.My ViewHolder class loooks like this

class ContactViewHolder extends RecyclerView.ViewHolder  {

    public TextView contactName,contactNumber;
    public ImageButton removeContact;
    private  List<Contact> contacts1;
    private ContactsAdapter contactsAdapter1;

    public ContactViewHolder(View itemView, ContactsAdapter contactsAdapter, final List<Contact> contacts) {
        super(itemView);
        this.contacts1=contacts;
        this.contactsAdapter1=contactsAdapter;
        contactName= (TextView) itemView.findViewById(R.id.tv_contactName);
        contactNumber= (TextView) itemView.findViewById(R.id.tv_contactNumber);
        removeContact= (ImageButton) itemView.findViewById(R.id.ib_removeItem);
        removeContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contacts1.remove(getPosition());
                contactsAdapter1.notifyItemRemoved(getPosition());
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM