how to create custom contact list with checkbox

后端 未结 1 1161
无人及你
无人及你 2020-12-16 08:30

how to create custom contact list with checkbox to select multiple contacts from list in android

public class AddFromContacts extends Activity {
ArrayList<         


        
相关标签:
1条回答
  • 2020-12-16 08:59

    Display.java

    public class Display extends Activity implements OnItemClickListener{
    
        List<String> name1 = new ArrayList<String>();
        List<String> phno1 = new ArrayList<String>();
        MyAdapter ma ;
        Button select;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display);
    
            getAllContacts(this.getContentResolver());
            ListView lv= (ListView) findViewById(R.id.lv);
                ma = new MyAdapter();
                lv.setAdapter(ma);
                lv.setOnItemClickListener(this); 
                lv.setItemsCanFocus(false);
                lv.setTextFilterEnabled(true);
                // adding
               select = (Button) findViewById(R.id.button1);
            select.setOnClickListener(new OnClickListener()
            {
    
                @Override
                public void onClick(View v) {
                        StringBuilder checkedcontacts= new StringBuilder();
    
                    for(int i = 0; i < name1.size(); i++)
    
                        {
                        if(ma.mCheckStates.get(i)==true)
                        {
                             checkedcontacts.append(name1.get(i).toString());
                             checkedcontacts.append("\n");
    
                        }
                        else
                        {
    
                        }
    
    
                    }
    
                    Toast.makeText(Display.this, checkedcontacts,1000).show();
                }       
            });
    
    
        }
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
             ma.toggle(arg2);
        }
    
        public  void getAllContacts(ContentResolver cr) {
    
            Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            while (phones.moveToNext())
            {
              String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
              String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
              name1.add(name);
              phno1.add(phoneNumber);
            }
    
            phones.close();
         }
        class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
        {  private SparseBooleanArray mCheckStates;
           LayoutInflater mInflater;
            TextView tv1,tv;
            CheckBox cb;
            MyAdapter()
            {
                mCheckStates = new SparseBooleanArray(name1.size());
                mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return name1.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
    
                return 0;
            }
    
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View vi=convertView;
                if(convertView==null)
                 vi = mInflater.inflate(R.layout.row, null); 
                 tv= (TextView) vi.findViewById(R.id.textView1);
                 tv1= (TextView) vi.findViewById(R.id.textView2);
                 cb = (CheckBox) vi.findViewById(R.id.checkBox1);
                 tv.setText("Name :"+ name1.get(position));
                 tv1.setText("Phone No :"+ phno1.get(position));
                 cb.setTag(position);
                 cb.setChecked(mCheckStates.get(position, false));
                 cb.setOnCheckedChangeListener(this);
    
                return vi;
            }
             public boolean isChecked(int position) {
                    return mCheckStates.get(position, false);
                }
    
                public void setChecked(int position, boolean isChecked) {
                    mCheckStates.put(position, isChecked);
                }
    
                public void toggle(int position) {
                    setChecked(position, !isChecked(position));
                }
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
    
                 mCheckStates.put((Integer) buttonView.getTag(), isChecked);         
            }   
        }   
    }
    

    display.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
       <ListView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_above="@+id/button1"
    
           android:id="@+id/lv"/>
    
         <Button
             android:id="@+id/button1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
             android:text="Select" />
    
    </RelativeLayout>
    

    row.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" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="34dp"
            android:text="TextView" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/checkBox1"
            android:layout_alignLeft="@+id/textView1"
            android:text="TextView" />
    
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/textView1"
            android:layout_marginRight="22dp"
            android:layout_marginTop="23dp" />
    
    </RelativeLayout>
    

    Explanation:

    The above uses a Custom Adapter. A custom layout row.xml with 2 textviews and one checkbox is inflated for each row. getAllContacts() will get all contacts from the contacts list and you store them in a list.

    Custom Adapter displays the items in a custom layout inflated for each row.

    When you check the check box and click display displays a toast with selected contacts name.

    Use a view holder for smooth scrolling and performance. I have not used a view holder.

    http://developer.android.com/training/improving-layouts/smooth-scrolling.html

    Snap shot

    enter image description here

    0 讨论(0)
提交回复
热议问题