0

I'm trying to obtain a list of contacts from the native database with their Display Name and Phone Number and display each one in a check Box but it doesn't work with me

Here is the query I've been working on

LinearLayout ll;
CheckBox ch1;
String name;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main1);
Button save = (Button) findViewById(R.id.bSave);
Button retur = (Button) findViewById(R.id.bReturn); 
retur.setOnClickListener(this);
ch1= (CheckBox)findViewById(R.id.checkBox1);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
Cursor phones = cr.query(Phone.CONTENT_URI, null,null,null,null);   
String number= phones.getString(phones.getColumnIndex(Phone.NUMBER));  
String contactId =cursor.getString(cursor.
           getColumnIndex(ContactsContract.Contacts._ID)); 
 ch1.setText(contactId+"  :  "+number);    
}

help

Wael Ilahi
  • 41
  • 3

2 Answers2

1

Make sure you're sending an actual string to your checkbox. I just tested it and YES you can setText on a checkbox. I dont know why thinksteep says you cant. try this:

ch1.setText(String.valueOf(contactId) + " : " + String.valueOf(number));

bwoogie
  • 4,339
  • 12
  • 39
  • 72
  • Yes i tested it but always there is an error...I think the the error is coming from here : ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null); Cursor phones = cr.query(Phone.CONTENT_URI, null,null,null,null); String number= phones.getString(phones.getColumnIndex(Phone.NUMBER)); String contactId =cursor.getString(cursor. getColumnIndex(ContactsContract.Contacts._ID)); ch1.setText(contactId+" : "+number); } – Wael Ilahi Feb 19 '12 at 20:50
  • well thats quite a bit of code... what line does the logcat say its on? what error? – bwoogie Feb 19 '12 at 20:54
  • also make sure you close your cursor when you're done with it. – bwoogie Feb 19 '12 at 20:54
  • the errors from the logcat-------> 1/ 02-19 22:12:09.427: E/AndroidRuntime(3556): java.lang.RuntimeException: Unable to start activity ComponentInfo{wael.ilahi.pfe/wael.ilahi.pfe.Secure1}: java.lang.IllegalArgumentException: Invalid column data1 2/02-19 22:12:09.427: E/AndroidRuntime(3556): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) – Wael Ilahi Feb 19 '12 at 21:17
0

Check boxes represent only two states (true/false). You can't set text to check boxes. Refer android documentation for checkbox. You may need to reconsider your stratagey.

kosa
  • 65,990
  • 13
  • 130
  • 167