ListView
適配器確實奇怪的事情。方法getChecked()
(下面顯示的適配器代碼)不會返回我期望的值。彷彿他遲到了一步。ListView適配器行爲奇怪
爲了解,我將解釋:我使用的是自定義ListView
,每個項目包含一個CheckBox
(和其他Views
)。我想按下按鈕顯示選中元素的位置。這使得方法getChecked()
。 但是會發生以下情況:我檢查第二和第四列表項並單擊按鈕,結果 - [],然後檢查第五項,結果 - [2,4],然後清除所有CheckBoxes
,結果[2,4,5]當我再次點擊按鈕時,我收到 - []。結果晚了一步。
public class ContactAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private boolean isCheckBoxVisible;
private View view;
private int[] colors = {Color.BLACK, Color.BLUE, Color.RED, Color.GRAY, Color.GREEN};
private int currentMaleColor;
private int currentFemaleColor;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.contacts = contacts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
isCheckBoxVisible = false;
setColors();
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private Contact getContact(int position) {
return (Contact) getItem(position);
}
public void setCheckBoxVisibility(boolean isVisible) {
isCheckBoxVisible = isVisible;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.contact_item, parent, false);
}
CheckBox cb = (CheckBox) view.findViewById(R.id.lv_box);
if (isCheckBoxVisible) {
cb.setVisibility(View.VISIBLE);
} else {
cb.setVisibility(View.INVISIBLE);
}
Contact c = getContact(position);
((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
((ImageView) view.findViewById(R.id.lv_img)).setImageBitmap(c.getPhoto());
if (c.getSex().equals("Man")) {
view.setBackgroundColor(currentMaleColor);
} else {
view.setBackgroundColor(currentFemaleColor);
}
boolean isCheck = ((CheckBox) view.findViewById(R.id.lv_box)).isChecked();
contacts.get(position).setChecked(isCheck);
return view;
}
public ArrayList<Integer> getChecked() {
notifyDataSetChanged();
ArrayList<Integer> IDs = new ArrayList<Integer>();
for (Contact c : contacts) {
if (c.isChecked()) IDs.add(c.getID());
}
return IDs;
}
private void setColors() {
int colorM = Integer.parseInt(MainActivity.sp.getString("lp_colorM", "0"));
int colorW = Integer.parseInt(MainActivity.sp.getString("lp_colorW", "0"));
currentMaleColor = colors[colorM];
currentFemaleColor = colors[colorW];
}
}
在我的活動我用我的轉接器,像這樣:
@Override
protected void onResume() {
super.onResume();
adapter = new ContactAdapter(getApplicationContext(), contacts);
list.setAdapter(adapter);
}
我的按鈕的代碼:
ArrayList<Integer> al = adapter.getChecked();
Toast.makeText(this, al.toString(), Toast.LENGTH_LONG).show();
你覺得這個怎麼樣?我會很樂意提供任何幫助。
'contacts.get(位置).setChecked(isCheck);如果聯繫人僅在刷新視圖時才更新狀態,而不是在單擊視圖時更新狀態。使用CB上的checkedchangedlistener立即檢測變化。 – njzk2 2015-02-10 14:32:40
好主意,** njzk2 **。我會盡力去做。但後來我有一個問題 - 爲什麼現在的方法getChecked()會給出遲到的結果?我認爲如果適配器沒有更新,這種方法現在不應該工作。 – Fredisson 2015-02-10 16:23:05
,因爲getChecked會將調用的結果返回給'setChecked',由於它只根據convertView的狀態進行更新而被延遲。 (如果你有足夠的項目滾動,它會給出更奇怪的結果。) – njzk2 2015-02-10 16:25:24