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