如何在Baseadapter获得职位

时间:2015-12-13 10:08:42

标签: android android-listview baseadapter android-viewholder

我从服务器获得响应并在listview中显示,但是如何在baseadapter中获取位置,我在listitem中有一个imageview,我想根据选择得到userid,现在我在每个listitem中得到6个最后一个id当我点击imageview

这是我的回复

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

        },
        {
            "user_id": "6",

        }
    ]
}

我的适配器

 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 个答案:

答案 0 :(得分:0)

我已编辑您的代码并检查并尝试

         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;
        }

    }