【问题标题】:Setting tag for listview item when using SimpleAdapter使用 SimpleAdapter 时为列表视图项设置标签
【发布时间】:2013-06-15 22:36:54
【问题描述】:

我想在每个 ListView 项目中存储一些数据(比如说 ID),然后在 clickItem 侦听器上检索它。

如果我创建自己的适配器,我知道该怎么做。但是如果我使用SimpleAdapter,是否可以为每个项目设置一个唯一的标签?

【问题讨论】:

  • 你能解释一下怎么做吗? :)

标签: android


【解决方案1】:

如果不覆盖至少getView(),您将无法使用每个适配器的视图回收器设置标签。

但是,您可以简单地传递带有可见性设置为 GONEINVISIBLE 的 TextView 的自定义布局,并从地图列表 (List<Map<String, ?>>) 中绑定数据。稍后您可以轻松地在 OnItemClickListener 中获取此 TextView。

【讨论】:

  • 感谢您的回答,但我对 setTag 特别感兴趣。
  • 我不知道您是否考虑将自定义 ViewBinder 设置为可接受的方法,但这为您提供了以回收者安全的方法访问每一行的布局(和 setTag())。
【解决方案2】:

如果您想通过这样的标签唯一地识别 imageview,我认为这将对您有所帮助

     //add this in your getview() method
   ImageView imageView = new ImageView(_context);
   imageView.setTag(1);

然后在 listview/imageView 的点击上检查它的标签,像这样

  listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

                  Tag = (Integer) arg1.getTag();

} }

【讨论】:

    【解决方案3】:

    SimpleAdapter 中有一个方法可以做到这一点。它被称为 ViewBinder。尝试在“SimpleAdapter adapter = new SimpleAdapter(this, Maps, R.layout.search, from, to);”之后立即包含此代码在“setListAdapter(adapter);”之前(在你的 onCreate 方法中)。

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //...
    
        String[] from = new String[] {"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN"};
    
    int[] to = new int[] { R.id.textView_1, R.id.textView_2, R.id.textView_3, R.id.textView_4, R.id.textView_5, R.id.textView_6, R.id.textView_7};
    
    SimpleAdapter adapter = new SimpleAdapter(this, Maps, R.layout.search, from, to);
    
    
        SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
        @Override
    public boolean setViewValue(View view, Object object, String value) {
    System.out.println("view= "+view);
        System.out.println("view.toString()= "+ view.toString());
        System.out.println("view.getId()= "+ view.getId()); //The return value will be decimal (not hexadecimal). You can have this value as a global string for later use.
        System.out.println("view.getVisibility()= "+ view.getVisibility());
        System.out.println("view.equals((TextView) view.findViewById(R.id. textView_5))= "+ view.equals((TextView) view.findViewById(R.id.textView_5)));
        if (view.equals((TextView) view.findViewById(R.id.textView_5)))
                {
                    TextView textView_five = (TextView) view.findViewById(R.id. textView_5);
                        //Change color/answer/etc for textView_5
                }
    
    //OR
              if (view instanceof TextView) {
                        //Do stuff
                        return true;
                    }
    
                    return false;
                }
            };
    
    
    adapter.setViewBinder(binder);
    
        setListAdapter(adapter);    
    }//End of onCreate
    

    将为每个 R.id.textView_1、R.id.textView_2、R.id.textView_3、R.id.textView_4、R.id.textView_5、R.id.textView_6、R.id 调用 setViewValue 方法。您在“适配器”中拥有的 textView_7。 setViewValue 方法将在每个视图/每次绘制上述 R.id 之一时被调用。

    然后,当用户单击 ListView 项目之一并且您想要更改它时,覆盖 onListItemClick 方法。

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    if (v.equals((TextView) v.findViewById(R.id.textView_5)))
        {
    TextView textView_five = (TextView) v.findViewById(R.id. textView_5);
        textView_five.setText(“stuff”); 
        //Change color/answer/etc for textView_5
                }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 2013-06-07
      • 2012-08-15
      相关资源
      最近更新 更多