我有一个ListView
,当用户单击其中一个项目时,我希望该项目变为蓝色。为了做到这一点,在活动的onCreate()
方法中ListView
,我为用户点击设置了一个监听器。
m_listFile=(ListView)findViewById(R.id.ListView01);
m_listFile.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
arg0.getChildAt(arg2).setBackgroundColor(Color.BLUE);
}
});
对于第一个可见项目,一切正常,但是当我滚动列表时,我有一个
NullPointerException
at arg0.getChildAt(arg2).setBackgroundColor(...)
,即使该arg2
值具有正确的项目索引位置。
我ListView
有一个两行项目结构,当我加载时ListView
我使用这个适配器:
SimpleAdapter sa = new SimpleAdapter(
getApplicationContext(),
expsList,
R.layout.listelement,
new String[] { "screen_name","text" },
new int[] { R.id.Name, R.id.Value}) {
};
m_listFile.setAdapter(sa);
我不明白如何解决这个问题。我能得到一些帮助吗?