Reputation: 1
In my android application i define a boolean Arraylist to save which items are checked in my ListView, so i can recover the ListView state.
public static ArrayList<Boolean> DataChecked = new ArrayList<Boolean>();
I was trying to change the DataChecked inside this listener, i found on web. This listener alows me to check and uncheck items, i added the last line:
public void onItemSelected(AdapterView parentView, View v, int position, long id) {
CheckedTextView textView = (CheckedTextView)v;
textView.setChecked(!textView.isChecked());
//changed
DataChecked.set(position,textView.isChecked());
}
public void onNothingSelected(AdapterView parentView) {
}
The check and uncheck stuff works fine. But the line i added does nothing, it doesn't update my DataChecked. Then i implemented this listener, and used both:
public void onItemClick(AdapterView parentView, View v,int position, long id) {
CheckedTextView textView = (CheckedTextView)v;
DataChecked.set(position,textView.isChecked());
}
Now the function i wanted (change DataChecked arraylist) works FINE! But i don't want two listeners, i want to implement this line on the first listener:
DataChecked.set(position,textView.isChecked());
So why is this happening? Have a clue?
Thanks!
Upvotes: 0
Views: 2731
Reputation: 6205
Be sure that your listview is using that listener.
// Use "this" if your Activity is implementing OnItemClickListener
myListView.setOnItemClickListener(this);
or
myListView.setOnItemClickListener(myOnItemClickListener);
or
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
CheckedTextView textView = (CheckedTextView)v;
textView.setChecked(!textView.isChecked());
//changed
DataChecked.set(position,textView.isChecked());
}
});
Upvotes: 1