I’ve a ListView and when the user clicks on one of its items I want that item to become blue. In order to do this, in the onCreate() method of the ListView activity, I’ve set a listener for the user clicks.
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);
}
});
Everything works fine for the first visible items, but when I scroll the list, I’ve a
NullPointerException at arg0.getChildAt(arg2).setBackgroundColor(...), even if the arg2 value has the right item index position.
My ListView has a two lines item structure, when I load the ListView I use this adapter:
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);
I don’t understand how to solve this problem. Can I get some help?
You could extend
SimpleAdapterlike this:Then in your activity add a field like this:
and set the adapter:
When you click the row: