該問題可以通過保持CheckBox
狀態輕鬆解決。自定義和解釋的完整的樣例類代碼相同的是below_
public class AreaDataAdapter extends ArrayAdapter<String> {
private List<String> areaList;
private Activity context;
ArrayList<Boolean> positionArray;
public AreaDataAdapter(Context context, int textViewResourceId,
List<String> offersAreaList) {
super(context, textViewResourceId, offersAreaList);
// TODO Auto-generated constructor stub
this.context=context;
this.areaList = offersAreaList;
positionArray = new ArrayList<Boolean>(offersAreaList.size());
for(int i =0;i<offersAreaList.size();i++){
positionArray.add(false);
}
}
public AreaDataAdapter(Activity context, List<String> offersAreaList) {
super(ShowOffersActivity.this, R.layout.filterlist_row, offersAreaList);
this.context=context;
this.areaList=offersAreaList;
positionArray = new ArrayList<Boolean>(offersAreaList.size());
for(int i =0;i<offersAreaList.size();i++){
positionArray.add(false);
}
}
public View getView(final int position, View convertView,ViewGroup parent) {
View row=convertView;
FilterViewHolder holder;
if (row==null) {
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.filterlist_row, parent, false);
holder = new FilterViewHolder();
holder.filterCheckBox = (CheckBox)row.findViewById(R.id.filter_checkbox);
holder.filterCheckBox.setTypeface(fontFace);
row.setTag(holder);
} else {
//holder = (View) row;
holder = (FilterViewHolder) row.getTag();
/* When a listview recycles views , it recycles its present state as well as listeners attached to it.
* if the checkbox was checked and has a onCheckedChangeListener set, both will remain a part of
* recycled view based on position. So it is our responsibility to reset all states and remove
* previous listeners.
* The listener was removed as below:-
*/
holder.filterCheckBox.setOnCheckedChangeListener(null);
}
holder.filterCheckBox.setText(areaList.get(position));
holder.filterCheckBox.setFocusable(false);
holder.filterCheckBox.setChecked(positionArray.get(position));
holder.filterCheckBox.setText(areaList.get(position));
holder.filterCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//change state of concern item in 'positionArray' array to 'True'
positionArray.set(position, true);
//add the checked item in to area list which used to filter offers.
filterOffersByThisAreaList.add(areaList.get(position));
} else {
//change state of concern item in 'positionArray' array to 'True'
positionArray.set(position, true);
//remove the unchecked item in to area list which used to filter offers.
filterOffersByThisAreaList.remove(areaList.get(position));
}
}
});
return row;
}
}
定製行(filterlist_row)
爲ListView
含有這些CheckBox
s是作爲below_
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="@+id/filter_checkbox"
style="@style/CodeFont"
android:button="@drawable/bg_custom_checkbox"
android:text="Indian" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#c9c9c3" />
</LinearLayout>
定製CheckBox
backgroung是below_
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_s" android:state_checked="true"></item>
<item android:drawable="@drawable/checkbox_ns" android:state_checked="false"></item>
</selector>
好吧,所以我選擇了一個。在我使用getView返回視圖之前,我做了「checkBox.setChecked(false);」。但是,現在,當我檢查複選框時,向下滾動並向上滾動,不再檢查。我知道問題出在我的實施上,而不是你的概念。我究竟做錯了什麼? – 2010-09-07 03:26:39
當用戶選中複選框時,您需要存儲該項目的複選框已被選中,在代碼中的某處。例如,如果您的ListView中有40個項目,則可以使用布爾數組來存儲是否選中第i個複選框。然後在'getView'上做checkBox.setChecked(booleanArray [position])。 – yuku 2010-09-07 03:56:33
也許是因爲它太晚了,但我不知道如何實際記錄位置。在getView方法中,我可以擁有該位置,但在需要實際存儲位置的OnClick中,我無法再訪問它。 – 2010-09-07 04:11:33