ListView适配器行为奇怪

时间:2015-02-10 12:15:24

标签: android listview android-listview adapter listview-adapter

ListView适配器做了一些奇怪的事情。方法getChecked()(如下所示的适配器代码)不返回我期望的值。好像他迟到了一步。

为了便于理解,我将解释:我使用的是自定义ListView,每个项目都包含一个CheckBox(以及其他Views)。当我按下按钮显示已检查元素的位置时,我想做。这使得方法getChecked()成为可能。 但是会发生以下情况:我检查第2和第4个列表项并单击按钮,结果 - [],然后我检查第5项,结果 - [2,4],然后我清理所有{{1} },结果[2,4,5]当我再次点击按钮时,我收到 - []。结果迟了一步。

CheckBoxes

在我的活动中,我使用我的适配器,如下:

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);
}

您如何看待这个?我会很高兴得到任何帮助。

3 个答案:

答案 0 :(得分:0)

如果convertView == null

,请尝试获取视图状态
 if (convertView == null) {
            holder = new Holder();
            convertView = mInflator.inflate(R.layout.row_viewproduct, null);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

//设置要从持有人

查看的数据

答案 1 :(得分:0)

这不是答案,但想说你可以在下面使用

private void setColors() {
        int colorM = Integer.parseInt(MainActivity.sp.getString("lp_colorM", "0"));
        int colorW = Integer.parseInt(MainActivity.sp.getString("lp_colorW", "0"));

        if(colorM < 5){
            currentMaleColor = colors[colorM];
        }

        if(colorW < 5){
            currentFemaleColor = colors[colorM];
        }  
    }

要回答您的问题,请查看视图持有者模式 http://ricston.com/blog/optimising-listview-viewholder-pattern/

答案 2 :(得分:0)

根据njzk2的建议,我改变了它:

boolean isCheck = ((CheckBox) view.findViewById(R.id.lv_box)).isChecked();
contacts.get(position).setChecked(isCheck);

在此:

CheckBox checkBox = (CheckBox) view.findViewById(R.id.lv_box);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        contacts.get(position).setChecked(isChecked);
    }
});

现在有效:)