Question

contact on list view with select all option and search option on android i have tried a lot of ways but no matter to find a list view with name,phone no and check box for everyone and also a select all button and search option

public void onTextChanged(CharSequence s,int start, int before, int count)
                    {
                        textlength = et.getText().length();
                    //  et.setText(textlength);
                        if(et.getText().toString().equalsIgnoreCase("helloo")){
                        //  Toast.makeText(getApplicationContext(),"jd",1000).show();
                            for (int i = 0; i < name1.size(); i++)
                            {
                            if (textlength <= name1.size())
                            {
                                Toast.makeText(getApplicationContext(),"searching.....inside",1000).show();
                            if(et.getText().toString().equalsIgnoreCase((String)((CharSequence) name1).subSequence(0,textlength)))
                            {
                            //  array_sort.addAll(name1);
                                                                                                                            }
                                                                                                            }

                                if(et.getText().toString().equalsIgnoreCase(
                                        (String)(CharSequence) name1)){
                                    Toast.makeText(getApplicationContext(),"searching.....",1000).show();
                                }

                                }
                            lv.setAdapter(new ArrayAdapter<String>
                            (contact_fetch.this,
                                    android.R.layout.simple_list_item_1, array_sort));


                        }
                    }
                });
Was it helpful?

Solution

I had the same problem.Finally I solved with this code. Hope It will help you :)

    //layout function
    public void lay_out(List<String> exp1,int k){
    LayoutInflater layoutInflater = 
                (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                container.removeAllViews();
                for(int i=0;i<exp1.size();i++){     
                    final View addView = layoutInflater.inflate(R.layout.row, null);
                    TextView tv=(TextView)addView.findViewById(R.id.textView1);
                    final CheckBox cb=(CheckBox) addView.findViewById(R.id.checkbox1);
                    if(k==0)
                        {
                            cb.setChecked(false);
                        }
                    else
                        {
                            cb.setChecked(true);
                        }
                    tv.setText(name1.get(i));
                    cb.setText(phno1.get(i));
                    container.addView(addView);
                    cb.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            //  Toast.makeText(getApplicationContext(),cb.getText().toString(),10).show();
                            if(cb.isChecked()==true){ 
                                if(checkedcontacts.length()<0)
                                {
                                    for(int i=0;i<=checkedcontacts.length();i++)
                                    {
                                        if(!checkedcontacts.equals(cb.getText().toString()))
                                        {
                                            // insertion
                                            checkedcontacts.append(cb.getText().toString());
                                            checkedcontacts.append("\n");
                                        }
                                    }
                                }
                                else{
                                checkedcontacts.append(cb.getText().toString());
                                checkedcontacts.append("\n");
                                }
                            }
                            else if(cb.isChecked()==false){
                                s_all.setChecked(false);
                                try{
                                    Toast.makeText(getApplicationContext(),cb.getText().toString(),10).show();
                                    //Pattern.compile(cb.getText().toString()).matcher(checkedcontacts).replaceAll("");
                                        checkedcontacts.delete(checkedcontacts.indexOf(cb.getText().toString()),
                                                cb.getText().toString().length());
                                }
                                catch(Exception e){
                                    System.out.println("error on array"+e);
                                }
                            }
                        }
                    });
            container.setVisibility(View.VISIBLE);
                   }
    }

OTHER TIPS

Try this code to get all contacts with checkboxex:

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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();
            System.out.println(".............." + ma.mCheckStates.size());
            for (int i = 0; i < name1.size(); i++)

            {
                if (ma.mCheckStates.get(i) == true) {
                    checkedcontacts.append(name1.get(i).toString());
                    checkedcontacts.append("\n");

                } else {
                    System.out.println("Not Checked......"
                            + name1.get(i).toString());
                }

            }

            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));
        System.out.println(".................." + phoneNumber);
        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);
        TextView tv = (TextView) vi.findViewById(R.id.contact_name);
        tv1 = (TextView) vi.findViewById(R.id.phone_number);
        cb = (CheckBox) vi.findViewById(R.id.checkBox_id);
        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);
        System.out.println("hello...........");
        notifyDataSetChanged();
    }

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

dispay.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/contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="John Doe"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/contact_name"
        android:text="(999)999-9999"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/checkBox_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top