自定义ListView的适配器

时间:2015-02-07 14:36:55

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

我创建一个联系人列表并使用自定义ListView(有一张照片,名称和不可见的CheckBox)。像这样:

<?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="horizontal"
    android:layout_margin="8dp">

<ImageView
    android:id="@+id/lv_img"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:src="@drawable/default_user"
    android:layout_weight="0"/>

<TextView
    android:id="@+id/lv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_margin="16dp"
    android:layout_gravity="center_vertical"
    android:textSize="24sp"/>

<CheckBox
    android:id="@+id/lv_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:visibility="invisible"
    android:layout_gravity="center"/>

</LinearLayout>

要管理这个,我使用适配器:

public class ContactAdapter extends BaseAdapter {

private Context context;
private LayoutInflater inflater;
private ArrayList<Contact> contacts;

private View view;

public ContactAdapter(Context context, ArrayList<Contact> contacts) {
    this.context = context;
    this.contacts = contacts;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@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 showCheckBox() {
        view = inflater.inflate(R.layout.contact_item, null, false);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    view = convertView;
    if (view == null) {
        view = inflater.inflate(R.layout.contact_item, parent, false);
    }

    Contact c = getContact(position);
    ((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
    ((ImageView) view.findViewById(R.id.lv_img)).setImageDrawable(c.getPhoto());
    return view;
}

public ArrayList<Contact> getChecked() {
    ArrayList<Contact> checkedContacts = new ArrayList<Contact>();
    for (Contact c : contacts) {
        if (c.isChecked()) checkedContacts.add(c);
    }
    return checkedContacts;
}
}

我需要在调用showCheckBox()时,所有CheckBox都变得可见,但只能看到ListView上的最后一个CheckBox。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试这个建议:

public class ContactAdapter extends BaseAdapter {

private Context context;
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private boolean isCheckBoxVisible;
private View view;

public ContactAdapter(Context context, ArrayList<Contact> contacts) {
    this.context = context;
    this.contacts = contacts;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.isCheckBoxVisible = false; 
}

@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 showCheckBox() {// if you want, you can add a boolean parameter to this method to change visibility
    isCheckBoxVisible = true;
    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)).setImageDrawable(c.getPhoto());
    return view;
}

public ArrayList<Contact> getChecked() {
    ArrayList<Contact> checkedContacts = new ArrayList<Contact>();
    for (Contact c : contacts) {
        if (c.isChecked()) checkedContacts.add(c);
    }
    return checkedContacts;
}
}