2011-09-22 53 views
5

我正在使用Sharkey的SeparatedListAdapter类来创建按部分分隔的ListView。Android - SeparatedListAdapter - 如何在onClick上获取准确的物品位置?

该课程效果很好,但我遇到的问题是在我的onListItemClick()处理程序中。如果我有以下切片列表:

--------- 
| A  | 
--------- 
| Alex | 
| Allan | 
--------- 
| B  | 
--------- 
| Barry | 
| Bart | 
| Billy | 
| Brian | 
--------- 
| C  | 
--------- 
etc... 

当你想点击一个项目在列表中,你获得该项目的position,然后用它做什么。

就我而言,我的列表是一个动态的人名列表。名称来自数据库,在进入列表之前将其放入List<CustomClass>。了解我点击的项目的位置对我来说很重要,因为我使用项目position来确定有关List对象中人员的其他信息。

现在,问题是这样的: 当您单击列表中的某个项目时,即使它们不可点击,标题也会计为项目。在上面的例子中,在列表中的位置如下

Alex = 1 
Allan = 2 
Barry = 4 
Bart = 5 
Billy = 6 
Brian = 7 

但在我原来的List对象的顺序是,像这样

Alex = 0 
Allan = 1 
Barry = 2 
Bart = 3 
Billy = 4 
Brian = 5 

所以每当我点击一个项目,像亚历克斯,我代码运行onClick处理程序,确定Alex在位置1处,然后从List而不是Alex的位置检索Allans信息。

你在这里看到delima吗?我应该如何解决这个问题?

回答

7

您可以使用setTag()作为listView中的每个项目。这可以通过在listAdapter中的getView()函数中返回视图之前添加此标记来完成。可以说你的列表适配器返回convertView,然后在列表适配器中。

public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) 
      { 
       convertView = mLayoutInflater.inflate(R.layout.morestores_list_item, null); 
       holder = new ViewHolder(); 

       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      // populate holder here, that is what you get in onItemClick. 
        holder.mNameofItem = "NameofaPerson"; 
      return convertView; 
     } 

static class ViewHolder { 
     TextView mName; 
     Button mAction; 
     ImageView mLogo; 
      // use this class to store the data you need, and use appropriate data types. 
      String mNameofItem. 
    } 

当过你的列表项的点击,使用

public void onItemClick(AdapterView<?> arg0, View convertView, int arg2, 
      long arg3) { 
       ViewHolder holder = (ViewHolder) convertView.getTag(); 
       // holder has what ever information you have passed. 
       String nameofItemClicked = holder.mNameofItem; 
       // access the information like this. 
    } 

这种方法并不需要在列表中的项目的位置,只是想让你知道不同的做法。

HTH。

+0

请问您能详细说明一下吗?谢谢 – Jovan

+0

检查编辑的答案。 –

1

我的解决方案不是获取列表中的位置,然后使用它从ListArray或Hash中获取对象,只是在适配器中获取该位置的对象,然后从中获取任何自定义参数或方法我需要的。现在很明显,但一开始没有意义。

UPDATE 它的码长一点,所以我只会给概述:

不是传递在列表项的字符串,传递一个自定义对象。可以说,这个对象有两个不同的值存储。名称(可通过公共方法getName()检索)和ID(可由getId()检索)。

您必须覆盖适配器类的getView()方法,并使其使用该对象的getName()方法,并使用该文本显示在列表项中。

然后,该项目被点击时,就得到了适配器对象在那个位置,然后拿到ID:

int id = adapter.get(position).getId(); 
// Then do something with the ID, pass it to an activity/intent or whatever. 

我也抛弃了SeparatedListViewAdapter赞成this。更好,更灵活的imo。

首先改变地图采取一种':

+0

你可以给我这个代码:“只需将对象放在适配器中的那个位置”谢谢 – Jovan

+0

我也放弃了SeparatedListViewAdapter来支持你提供的链接。简单得多。 :)。感谢那个链接。 – Veer

1
int idNo= adapter.get(position).getId(); 

你可以试试这个代码...

1

对于别人我最近也遇到了这个问题,并在这里结束了,我要解决ID”

public Map<String,?> createItem(String title, String caption, String id) { 
    Map<String,String> item = new HashMap<String,String>(); 
    item.put(ITEM_TITLE, title); 
    item.put(ITEM_CAPTION, caption); 
    item.put("id", id); // Add a unique id to retrieve later 
    return item;} 

添加部分一定要包括ID时二:

List<Map<String,?>> GeneralSection = new LinkedList<Map<String,?>>(); 
      GeneralSection.add(createItem(FirstName[GeneralInt] + " " + LastName[GeneralInt], Whatever[GeneralInt], UniqueId[GeneralInt])); 
//Unique id could be populated anyway you like, database index 1,2,3,4.. for example like above question. 
      adapter.addSection("GeneralSection ", new SimpleAdapter(getActivity(), GeneralSection , R.layout.list_complex, 
      new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption })); 

最后使用在ItemClickListener一个地图检索正确位置的“身份证”:

@Override 
     public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { 
      // TODO Auto-generated method stub 

      Map<String, String> map = (Map<String, String>) arg0.getItemAtPosition(position); 
      String UniqueId = map.get("id"); 
      Log.i("Your Unique Id is", UniqueId); 
//Can then use Uniqueid for whatever purpose 
//or any other mapped data for that matter..eg..String FirstName = map.get("ITEM_TITLE"); 


    } 

希望这可以帮助别人。

相关问题