如何使用自定义适配器在列表视图中显示的联系人列表中选择多个联系人?

时间:2017-07-06 08:57:53

标签: java android

我在互联网上花了四个小时但找不到解决我问题的解决方案。我有一个活动(不是主要活动),从电话中显示联系人列表。

这个应用程序正在运行,但我面临的问题是,当我滚动浏览显示的联系人列表时,会自动取消选择所选联系人,并选择一些随机联系人。

SelectContacts.java         公共类Select_Contacts扩展AppCompatActivity {

    List<ContactData> storeContacts = new ArrayList<>();
    String name, phoneNumber;
    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select__contacts);
        listview = (ListView) findViewById(R.id.contactsListView);
        Button contactsSelected = (Button) findViewById(R.id.contactsSelected);
        EditText searchContacts = (EditText) findViewById(R.id.SearchContacts);
        searchContacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        contactsSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Select_Contacts.this, TimeDatePicker.class);
                startActivity(intent);
            }
        });
        getContacts();

    }

    public void getContacts(){
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI , null, null, null, null);
        while (cursor.moveToNext()){
            ContactData c = new ContactData();
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = cursor.getString((cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            c.setName(name);
            c.setNumber(phoneNumber);
            storeContacts.add(c);
        }
        ListAdapter arrayadapter = new ContactsListCustomAdapter(this, storeContacts);
        listview.setAdapter(arrayadapter);
        cursor.close();

    }

布局文件 - activity_select_contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.instareminder.Select_Contacts">

    <EditText
        android:id="@+id/SearchContacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Select Contacts"
        android:textSize="20sp"
        android:layout_margin="10dp"
        android:layout_marginBottom="10dp"
        />
    <ListView
        android:id="@+id/contactsListView"
        android:layout_below="@id/SearchContacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="60dp"
        />

    <Button
        android:id="@+id/contactsSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pick Time and Date"
        android:layout_margin="10dp"
        android:layout_alignParentBottom="true"/>



</RelativeLayout>

自定义适配器布局文件 - contact_search_custom_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <TextView
        android:id="@+id/ContactName"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="dummy text"
        android:textSize="20sp"
        android:layout_marginLeft="10dp"
        />
    <TextView
        android:layout_marginLeft="10dp"
        android:id="@+id/ContactNumber"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="000000000"
        android:textSize="20sp"
        android:layout_below="@id/ContactName"/>
    <ImageView
        android:id="@+id/selectContactCheckBox"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/success"
        android:layout_toRightOf="@id/ContactName"
        android:layout_marginTop="15dp"/>

</RelativeLayout>

自定义适配器Java文件 - ContactsListCustomAdapter.java

public class ContactsListCustomAdapter extends ArrayAdapter<ContactData> {

    List<ContactData> contactList = new ArrayList<>();

    public ContactsListCustomAdapter(Context context, List<ContactData> c) {
        super(context, R.layout.contact_search_custom_adapter, c);
        this.contactList = c;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater customInflater = (LayoutInflater.from(getContext()));
            convertView = customInflater.inflate(R.layout.contact_search_custom_adapter, parent, false);
        }

            final ContactData contact = contactList.get(position);
            TextView nameTV = (TextView) convertView.findViewById(R.id.ContactName);
            TextView numberTV = (TextView) convertView.findViewById(R.id.ContactNumber);
            nameTV.setText(contact.getName());
            numberTV.setText(contact.getNumber());
            final ImageView selectContactCheckBox  = (ImageView) convertView.findViewById(R.id.selectContactCheckBox);

        selectContactCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (contactList.get(position).getSelected() == false){
                    contactList.get(position).setSelected(true);
                    selectContactCheckBox.setImageResource(R.drawable.successtrue);
                }else if(contactList.get(position).getSelected() == true){
                    contactList.get(position).setSelected(false);
                    selectContactCheckBox.setImageResource(R.drawable.success);
                }
            }
        });


        return convertView;
    }
}

联系信息的POJO - ContactData.java

public class ContactData {
    private String name;
    private String contactnumber;
    private boolean isSelected = false;

    public void setSelected(boolean select){ this.isSelected = select; }

    public boolean getSelected(){ return  this.isSelected; }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getNumber(){
        return contactnumber;
    }

    public void setNumber(String contactnumber){
        this.contactnumber = contactnumber;
    }


}

我想在列表视图中实现多个联系人的选择,以便所选联系人列表可以显示在另一个活动中。

我该如何实现?

0 个答案:

没有答案