简体   繁体   中英

Android Custom Contact ListView With checkbox

i am trying to implement a custom listview in android with checkbox. user can create a group of contacts which are checked in checkboxes i need help in implementing ViewHolder class and secondly how can i assign contacts to each list item

ContactList Single Row XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <TextView
        android:id="@+id/textViewContactName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/textViewContactNameResource"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/textViewContactNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/textViewContactNumberResource"
        android:textSize="15sp"
        android:layout_below="@+id/textViewContactName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <CheckBox
        android:id="@+id/checkBoxContactSelection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:layout_below="@+id/textViewContactName"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

[![enter image description here][1]][1]

InviteAndAddPeople.Java

public class InviteAndAddPeople extends ListActivity {

    ListView listViewContacts;
    Cursor cursor;
    private static final int read_Contacts_Permission_Request = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_and_add_people);
        this.setTitle("Invite Friends");
        View view = getLayoutInflater().inflate(R.layout.invite_add_header,null);

        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
        startManagingCursor(cursor);
        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};

        //int[] to = {R.id.textViewContactName, R.id.textViewContactNumber};

        ListAdapter listadapter = new CustomContactsAdapter(this,cursor ,from);
        //SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this,R.layout.contacts_list_view,cursor,from,to);



        setListAdapter(listadapter);

        listViewContacts = getListView();
        listViewContacts.addHeaderView(view);
        //listViewContacts.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
        //listViewContacts.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    }

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

    @Override
    public int getSelectedItemPosition() {
        return super.getSelectedItemPosition();
    }

    @Override
    public long getSelectedItemId() {
        return super.getSelectedItemId();
    }
}

CustomContactsAdapter.Java

public class CustomContactsAdapter extends ArrayAdapter<String> {

    Context context;
    String [] from;
    Cursor cursor;
    public CustomContactsAdapter(Context context,Cursor cursor ,String[] from) {
        super(context, R.layout.contacts_list_view,from);
        this.context = context;
        this.from = from;
        this.cursor = cursor;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return super.getView(position, convertView, parent);
    }
}

The problem is you are not holding the state of CheckBox , that's why you are not getting the checked values as you want.

  • Use List or ArrayList of your custom class or of HashMap to store data.
  • Use ViewHolder to instantiate and set the values to view.
  • Set the ViewHolder as a tag to View inside getView() and get the tag from View .
  • Check value of particular index of List and check the CheckBox accordingly.

Use adapter like below:

public class CustomContactsAdapter extends ArrayAdapter<Employee> {

Context context;
ArrayList<Employee> employees;
LayoutInflater inflator;

public CustomContactsAdapter(Context context, ArrayList<Employee> employees) {
    super(context, R.layout.list_item_employee);
    this.context = context;
    this.employees = employees;
    inflator = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //return super.getView(position, convertView, parent);

    View view = null;
    if (convertView == null) {

        view = inflator.inflate(R.layout.list_item_employee, null);
        final ViewHolder viewHolder = new ViewHolder();

        viewHolder.nameTextView = (TextView) view.findViewById(R.id.codeTextView);
        viewHolder.experienceEditText = (EditText) view.findViewById(R.id.experienceEditText);
        viewHolder.isMarriedCheckBox = (CheckBox) view.findViewById(R.id.marriedCheckBox);

        viewHolder.nameTextView.setText(employees.get(position).getName());

        // Text Changed Listener
        viewHolder.experienceEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (!s.toString().equals("")) {
                    Employee employee = (Employee) viewHolder.experienceEditText.getTag();
                    employee.setExperienceYear(Integer.valueOf(s.toString()));

                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        // Check Changed Listener
        viewHolder.isMarriedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Employee employee = (Employee) viewHolder.isMarriedCheckBox.getTag();
                employee.setIsMarried(buttonView.isChecked());
            }
        });


        viewHolder.experienceEditText.setTag(employees.get(position));
        viewHolder.isMarriedCheckBox.setTag(employees.get(position));
        view.setTag(viewHolder);


    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).experienceEditText.setTag(employees.get(position));
        ((ViewHolder) view.getTag()).isMarriedCheckBox.setTag(employees.get(position));
    }

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.experienceEditText.setText(String.valueOf(employees.get(position).getExperienceYear()));

    if (employees.get(position).isMarried() == true)
        holder.isMarriedCheckBox.setChecked(true);
    else
        holder.isMarriedCheckBox.setChecked(false);

    holder.nameTextView.setText(employees.get(position).getName());

    return view;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getPosition(Employee item) {
    return super.getPosition(item);
}

@Override
public Employee getItem(int position) {
    return employees.get(position);
}

@Override
public int getCount() {
    return employees.size();
}


static class ViewHolder {
    protected TextView nameTextView;
    protected CheckBox isMarriedCheckBox;
    protected EditText experienceEditText;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM