【问题标题】:Android - SeparatedListAdapter - How to get accurate item position on onClick?Android - 分离列表适配器 - 如何在 onClick 上获得准确的项目位置?
【发布时间】:2012-11-29 21:12:27
【问题描述】:

我正在使用 Sharkey 的 separatordListAdapter 类来获得一个按部分分隔的 ListView。

该课程效果很好,但我遇到的问题在于我的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

因此,每当我点击某个项目(例如 Alex)时,我的代码都会运行 onClick 处理程序,确定 Alex 位于 1 的位置,然后从 List 而不是 Alex 的位置检索 Allans 信息。

你看到这里的delim了吗?我应该采取什么方法来解决这个问题?

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以为listView 中的每个项目使用setTag()。这可以通过在从getView() 函数返回listAdapter 中的视图之前添加此标记来完成。假设您的列表适配器返回 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。

    【讨论】:

      【解决方案2】:

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

      更新 代码很长,我只做一个概述:

      不是为列表项传入一个字符串,而是传入一个自定义对象。假设这个对象存储了两个不同的值。名称(可通过公共方法 getName() 检索)和 ID(可通过 getId() 检索)。

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

      那么,当item被点击的时候,就从那个位置的adapter中获取object然后获取id:

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

      另外,我放弃了分离列表视图适配器,转而使用 this。更好、更灵活的 imo。

      【讨论】:

      • 你能给我这个“只需将对象放在适配器中的那个位置”的代码吗?谢谢
      • 我也放弃了separatedListViewAdapter,转而使用您提供的链接。简单得多。 :)。谢谢你的链接。
      【解决方案3】:
       int idNo= adapter.get(position).getId();
      

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

      【讨论】:

        【解决方案4】:

        对于其他任何人,我最近也遇到了这个问题并在这里结束,我通过以下方式解决了:

        首先更改地图以获取“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 中使用 Map 来检索正确位置的 'id':

          @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");
        
        
            }
        

        希望这对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-22
          • 1970-01-01
          相关资源
          最近更新 更多