使用SimpleAdapter时为listview项设置标记

时间:2012-09-11 16:38:31

标签: android

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

如果我创建自己的适配器,我知道如何做到这一点。但是,如果我使用SimpleAdapter

,是否可以为每个项目设置唯一标记

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

我认为如果你想通过像这样的标签唯一识别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();

} }

答案 2 :(得分:0)

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。每次View /每次正在绘制上述R.id中的一个时,将调用setViewValue方法。

然后,当用户单击其中一个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
            }
}