I have a ListView with set onItemClickListener:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// not important
if (!found) {
activity.addSelectedIngredient(ingred);
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
parent.getChildAt(position).setBackgroundColor(Color.WHITE);
}
}
The NullPointerException is thrown when parent hasn’t got child on selected position (e.g. 15). Why? How it’s possible that the element might not be present if she already selected it?
Edit:
if (!found) {
activity.addSelectedIngredient(ingred);
view.setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
view.setBackgroundColor(Color.WHITE);
}
getChildAt returns listView’s child. getChildAt position is not same position as in your adapter. You can have 1000 items in your adapter and only several childViews in listview because views are reusing.
I think you should change
to