【问题标题】:Separated list adapter - get the position of the item分隔列表适配器 - 获取项目的位置
【发布时间】:2014-09-21 05:12:52
【问题描述】:

我有一个包含 2 个部分的 SeperatedListAdapter,每个部分包含 6 个项目。

代码:

listView.setAdapter(adapter);
listView.setOnItemClickListener(listViewListener);

以这种方式添加节标题和项目:

adapter = new SeparatedListAdapter(this);
adapter.addSection(entry.getKey(), new ItemAdapter(this, 0, topics.toArray(array)));

OnItemClickListener listViewListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
          Employee emp = emps.get(position - 1); 
    }
};

我有 ArrayList 为:

items from section 1
    Anand - 0
    Sunil - 1
    Suresh - 2
    Dev - 3
    Faran - 4
    Khan - 5
items from section 2
    Samba - 6
    Surendra - 7
    Rajesh - 9
    Rakesh - 10
    Satish - 11

现在在OnItemClickListener 中,当我获得位置时,它也将节标题作为位置。

所以我以Employee emp = emps.get(position - 1); 的身份进行操作,但最多 6 个项目(我的数组列表中的 0-5 个)很好,但之后位置不正确。我该如何解决这个问题?

我需要以这种方式将位置传递给我的数组列表

Employee emp = emps.get(position - 1);

因为我会将员工对象传递给另一个类。

也看到这个:

Android - SeparatedListAdapter - How to get accurate item position on onClick?

【问题讨论】:

  • 你用的是settag方式吗?
  • @PankajKumar 没有我没用过
  • 好的,您可以分享适配器的代码以及您如何为部分设置适配器的代码吗?
  • @PankajKumar 在这里jsharkey.org/blog/2008/08/18/…
  • 已添加答案。您只需要使用Employee emp = (Employee) adapter.getItem(position); 即可获得正确的物品。

标签: android listview listadapter


【解决方案1】:

正如您在评论中提到的,您使用的是Separating Lists with Headers in Android 0.9 示例。

所以有一个方法进入adpater,

public Object getItem(int position) {  
        for(Object section : this.sections.keySet()) {  
            Adapter adapter = sections.get(section);  
            int size = adapter.getCount() + 1;  

            // check if position inside this section   
            if(position == 0) return section;  
            if(position < size) return adapter.getItem(position - 1);  

            // otherwise jump into next section  
            position -= size;  
        }  
        return null;  
    }  

返回正确的项目。

所以你只需要调用这个方法,变成OnItemClickListener就好了

OnItemClickListener listViewListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
            Employee emp = (Employee) adapter.getItem(position); // HERE is the code to get correct item.
}
};

将以下方法添加到SeparatedListAdapter

public Employee getItem(int position, ArrayList<Employee> lists) {  
        for(Object section : this.sections.keySet()) {  
            Adapter adapter = sections.get(section);  
            int size = adapter.getCount() + 1;  

            // check if position inside this section   
            if(position == 0) return lists.get(position);   
            if(position < size) return lists.get(position - 1);  

            // otherwise jump into next section  
            position -= size;  
        }  
        return null;  
    }  

并将其称为

Employee emp = adapter.getItem(position, emps);

【讨论】:

  • 是的,我试过它说 String 无法转换为我的员工
  • 是的,没错。如果您将 String 数组提供给适配器,那么给定的方法将返回对象,该对象将可类型转换为 String 而不是 Employee。要获取员工对象,您需要将员工数组传递给适配器。
  • 这就是我设置部分和项目的方式 adapter.addSection(entry.getKey(), new ItemAdapter(this, 0, topics.toArray(array)));
  • topics.toArray(array))的类型是什么?字符串还是员工数组?如果是Employee,那么你应该修改ItemAdapter适配器的getItem。如果没有,则将此方法添加到类中。
  • 它是一个字符串 ArrayList topics = entry.getValue();
猜你喜欢
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多