0

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

<?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> 

要管理这个问题,我使用适配器:调用showCheckBox()时,所有CheckBox显形,而成为对ListView中唯一可见的最后一个复选框

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

我需要。我该如何解决?

+0

你能澄清什么是你想在这里实现我din't明白你想要 – user1530779 2015-02-07 16:58:48

回答

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; 
} 
} 
+0

哦,太棒了什么!它工作得很好,谢谢。 – Fredisson 2015-02-07 19:22:29

+0

林间空地给你! – Rami 2015-02-07 19:33:18

相关问题