动态更改ListView元素

时间:2012-12-17 19:08:32

标签: android

我正在使用Listview在屏幕上的两个TextView中显示来自Cursor的三列信息。左侧TextView是静态的,而右侧的TextView是两种语言的翻译,在选择时可以切换。

我的问题是这个工作正常,直到我滚动ListView,此时适配器和ListView似乎变得不同步,导致片状行为,最后是空指针异常。

这是onListItemClick代码:

 public void onListItemClick(ListView l, View v, int position, long id){
    //first revert previous selection to English
    if(previous_position!=99){
      View vo =l.getChildAt(previous_position);
      TextView pt = (TextView)vo.findViewById(R.id.english);
      pt.setTextColor(Color.BLUE);
      pt.setText(previous_text);
    }

    //now show current selection in Hindi
    TextView t = (TextView) v.findViewById(R.id.translation);
    t.setTextColor(Color.RED);
    t.setText(gloCursor.getString(3));

    //finally remember where we changed things so we can revert them
    previous_position = position;
    previous_text = gloCursor.getString(3);

    // adapter data notification
    //adapter.notifyDataSetChanged();
    }

我已经注释掉了notifyDataStateChanged,因为在这种情况下它对我没用。

非常感谢任何洞察力或者更好的设计。

感谢, P上。

1 个答案:

答案 0 :(得分:0)

尝试将您的getChildAt代码替换为:

View vo = l.getChildAt(previous_position - l.getFirstVisiblePosition());