Android: Chaotic Selection In Listview
Solution 1:
Wow, its the first hurdle you face in your first ListView
. Don't worry. First of all understand how ListView
works, its important. Otherwise you can't master ListView
. In simplest word: ListView
creates few row View
s and utilizes them for every row. So if you have have 50 elements in your ListView
then may be only 10 times getView()
was called with convertView being null. So in short if you don't update the state of a view inside getView()
method for every position, it will really be a chaos. Also please use View Holder Pattern in your ListView
. It will drastically increase scrolling and will utilize less resources. More on View Holder Pattern http://www.jmanzano.es/blog/?p=166
UPDATE:
I said
So in short if you don't update the state of a view inside
getView()
method for every position, it will really be a chaos.
privateSparseBooleanArraycheckedPositions=newSparseBooleanArray();// class variable
listView.setOnItemClickListener(newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(checkedPositions.get(position)){
checkedPositions.put(position, false);
}else{
checkedPositions.put(position, true);
}
}
});
now in your getView()
((CheckedTextView)convertView.findViewById(R.id.checkedTextView)).setChecked(checkedPositions.get(position));
adjust this code according to your need. OR you can have a boolean variable in your BinLocation
to maintain checked positions. But remember and understand the point of maintaining state inside getView()
.
Post a Comment for "Android: Chaotic Selection In Listview"