Android - SeparatedListAdapter - How to get accurate item position on onClick?

前端 未结 4 918
野的像风
野的像风 2021-01-13 06:09

I\'m using Sharkey\'s SeparatedListAdapter class in order to have a ListView that is separated by sections.

The class works great, but the problem I\'m having is in

4条回答
  •  伪装坚强ぢ
    2021-01-13 07:01

    You could use setTag() for your every item in the listView. This can be done by adding this tag before you return the view from getView() function in the listAdapter. lets say your list adapter return convertView ,then in the list adapter.

        public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null)
                {
                    convertView = mLayoutInflater.inflate(R.layout.morestores_list_item, null);
                    holder = new ViewHolder();
    
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                // populate holder here, that is what you get in onItemClick.
                        holder.mNameofItem = "NameofaPerson";
                return convertView;
            }
    
    static class ViewHolder {
            TextView mName;
            Button mAction;
            ImageView mLogo;
                // use this class to store the data you need, and use appropriate data types.
                String mNameofItem.
        }
    

    when ever you get a click on the list item, use

    public void onItemClick(AdapterView arg0, View convertView, int arg2,
                long arg3) {
                    ViewHolder holder = (ViewHolder) convertView.getTag();
                    // holder has what ever information you have passed.
                    String nameofItemClicked = holder.mNameofItem;
                    // access the information like this.
        }
    

    This approach doesn't need the position of the item in the list, just wanted to let you know different approach.

    HTH.

提交回复
热议问题