如何在 Baseadapter 中获得位置

Posted

技术标签:

【中文标题】如何在 Baseadapter 中获得位置【英文标题】:How to get position in Baseadapter 【发布时间】:2016-03-18 22:13:24 【问题描述】:

我正在从服务器获得响应并将其显示在列表视图中,但是如何在 baseadapter 中获取位置,我在列表项中有一个图像视图,我想根据选择获取用户 ID,现在我在每个列表项中获得 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;
        

    

【问题讨论】:

怎么做??你能告诉我 @MikeM。我不知道该怎么做 它不工作..仍然给出 6 【参考方案1】:

我已经编辑了你的代码检查并尝试

         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;
        

    

【讨论】:

以上是关于如何在 Baseadapter 中获得位置的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中获取 BaseAdapter 的上下文

如何在android中使用baseadapter刷新自定义列表视图

滚动时的 BaseAdapter 列表视图位置

ros如何获得机器人在map中的朝向

Android BaseAdapter:位置不是真正的位置?

使用带有自定义 baseadapter 的 arraylist 错误地删除列表视图项位置