Search code examples
androidlistviewandroid-listviewlistviewitemlistview-adapter

Adapter for custom ListView


I create a list of contacts and use custom ListView (there is a photo, name and invisible CheckBox). Like this:

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

To manage this, I use the adapter:

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

I need to when calling showCheckBox () all CheckBoxes became visible, but becomes visible only the last CheckBox on the ListView. How do I fix this?


Solution

  • Try this suggestion:

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