【问题标题】:How to overcome change of position in Listview in android如何克服android中Listview中位置的变化
【发布时间】:2012-11-01 10:50:18
【问题描述】:

在基本适配器中,包含多个视图以及复选框,

如果我选择复选框,则获得一个位置,如果我滚动并取消选择相同的复选框,则获得另一个位置,但我需要相同的位置..如何获得这个?

【问题讨论】:

  • 没有看到任何代码就很难判断.. 这可能是 getView 方法的问题
  • 参考 API 演示(视图/列表/多项选择列表)。有史以来的最佳示例。

标签: android listview scroll adapter base


【解决方案1】:

在您的适配器类中

 public View getView(final int position, final View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
if (row == null) 
{
    LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.reports_list, null);
}

CheckBox cBox=(CheckBox)row.findViewById(R.id.cb1);

cBox.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
    if(cBox.isChecked())
    {
       System.out.println("position "+reportslistview.getPositionForView(cBox));
    }
}
});

【讨论】:

    【解决方案2】:

    这是因为您的适配器正在回收视图,而您没有正确处理它。确保您使用的是setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)setChoiceMode(ListView.CHOICE_MODE_SINGLE)

    如果你使用自定义列表项,请参考我的教程:CUSTOM LISTVIEW WITH ABILITY TO CHECK ITEMS

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 2011-11-02
      • 1970-01-01
      相关资源
      最近更新 更多