How to get position in Baseadapter

心不动则不痛 提交于 2019-12-12 03:17:25

问题


I am getting response from server and display it in listview,but how to get position in baseadapter,,i have one imageview in listitem and i want to get userid as per selection,right now i am getting 6 last id in everylistitem when i click on imageview

this is my response

 {
    "searchresult": [
        {
            "user_id": "5",

        },
        {
            "user_id": "6",

        }
    ]
}

MY adapter

 public class CustomAdapterAccept extends BaseAdapter{

        private Context context;
        private ArrayList<HashMap<String,String>> listData;
        private AQuery aQuery;

        private  String TAGs_NAME="user_fname";
        private  String TAG_FRIENDID="user_id";
        private String TAGs_IMAGES="user_pic";
        private  String TAGs_CITY="user_city";


        public CustomAdapterAccept(Context context,ArrayList<HashMap<String,String>> listData) {
            this.context = context;
            this.listData=listData;
            aQuery = new AQuery(this.context);
        }

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView( final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item_balloonationsearch, null);
                holder.propics = (ImageView) convertView.findViewById(R.id.list_item_ballonationsearch_propic);
                holder.txtproname = (TextView) convertView.findViewById(R.id.list_item_ballonationsearch_username);

                holder.addfriend=(ImageView) convertView.findViewById(R.id.addfriend_after_searchs);
                holder.city=(TextView) convertView.findViewById(R.id.serach_city);



                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

             holder.addfriend.setTag(position);
        holder.addfriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final int position = (int) view.getTag();
                frindsids=listData.get(position).get(TAG_FRIENDID);
                System.out.println("HWIHC ID"+frindsids);
            }
        });

            holder.txtproname.setText(listData.get(position).get(TAGs_NAME));
            holder.city.setText(listData.get(position).get(TAGs_CITY));
            aQuery.id(holder.propics).image(listData.get(position).get(TAGs_IMAGES),true,true,0,R.drawable.male);

            // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
            return convertView;
        }
        class ViewHolder{
            ImageView propics;
            TextView txtproname,city;
            TextView txtproid;
            TextView txtprofilecast;
            TextView txtprofileage;
            TextView txtprofileplace;
            public ImageView addfriend;
        }

    }

回答1:


I have edited your code check it and try

         holder.addfriend=(ImageView) convertView.findViewById(R.id.addfriend_after_searchs);
holder.addfriend.setTag(position);
                holder.city=(TextView) convertView.findViewById(R.id.serach_city);



                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

             holder.addfriend.setTag(position);
        holder.addfriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
  int posi = (Integer) view.getTag();

                frindsids=listData.get(posi).get(TAG_FRIENDID);
                System.out.println("HWIHC ID"+frindsids);
            }
        });

            holder.txtproname.setText(listData.get(position).get(TAGs_NAME));
            holder.city.setText(listData.get(position).get(TAGs_CITY));
            aQuery.id(holder.propics).image(listData.get(position).get(TAGs_IMAGES),true,true,0,R.drawable.male);

            // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
            return convertView;
        }
        class ViewHolder{
            ImageView propics;
            TextView txtproname,city;
            TextView txtproid;
            TextView txtprofilecast;
            TextView txtprofileage;
            TextView txtprofileplace;
            public ImageView addfriend;
        }

    }


来源:https://stackoverflow.com/questions/34249960/how-to-get-position-in-baseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!