Why do the selected contacts not appear in the textview?
I am creating a multiple choice layout for contact list. I successfully pick the contacts. However, instead of displaying the contact name in text view,it appear as String value. I just want the contact name to appear in TextView not the String values.
I got 2 classes. One class is to pick contact. While the other class is for browsing and selecting the contacts.
Selected Contact Activity
package com.droidnova.android.samples;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.Button;
public class SelectContact extends Activity {
private static final int PICK_CONTACT = 0;
private Button btnPick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
btnPick = (Button)findViewById(R.id.button);
btnPick.setOnClickListener(new MyOnClickListener());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT) {
if (resultCode == RESULT_OK) {
Cursor contact = getContentResolver().query(data.getData(), null, null, null, null);
contact.moveToFirst();
String name = contact.getString(contact.getColumnIndexOrThrow(People.NAME));
}
}
}
private class MyOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(Intent.ACTION_PICK),PICK_CONTACT);
}
}
}
Contacts Activity
package com.droidnova.android.samples;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Contacts extends ListActivity {
ListView list;
TextView Selection;
private static String[] PROJECTION = new String[]
{
People._ID,
People.NAME,
People.NUMBER,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(android.R.id.list);
Cursor cursor = getContentResolver().query(People.CONTENT_URI,
PROJECTION, null, null, null);
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_multiple_choice,
cursor,new String[]{People.NAME},new int[]{android.R.id.text1});
getListView().setChoiceMode(2);
setListAdapter(adapter);
Selection = (TextView)findViewById(R.id.selection);
}
@Override
protected void onListItemClick(ListView parent,View v,int position,long id){
Selection.setText("");
SparseBooleanArray chosen = parent.getCheckedItemPositions();
for(int i=0;i<chosen.size();i++)
{
Log.d("Selection", "index:"+i+";key:"+chosen.keyAt(i) +";value:"+chosen.valueAt(i)
+";"+PROJECTION[chosen.keyAt(i)]);
if(chosen.valueAt(i)){
Selection.append(PROJECTION[chosen.keyAt(i)]+"");
}
}
}
}
This is my logcat:
12-02 03:10:49.840: INFO/ActivityManager(53): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.droidnova.android.samples/.SelectContact }
12-02 03:10:49.930: INFO/ActivityManager(53): Start proc com.droidnova.android.samples for activity com.droidnova.android.samples/.SelectContact: pid=715 uid=10025 gids={1015}
12-02 03:10:49.930: DEBUG/AndroidRuntime(709): Shutting down VM
12-02 03:10:49.930: DEBUG/dalvikvm(709): DestroyJavaVM waiting for non-daemon threads to exit
12-02 03:10:49.940: DEBUG/dalvikvm(709): DestroyJavaVM shutting VM down
12-02 03:10:49.940: DEBUG/dalvikvm(709): HeapWorker thread shutting down
12-02 03:10:49.940: DEBUG/dalvikvm(709): HeapWorker thread has shut down
12-02 03:10:49.940: DEBUG/jdwp(709): JDWP shutting down net...
12-02 03:10:49.940: INFO/dalvikvm(709): Debugger has detached; object registry had 1 entries
12-02 03:10:49.960: DEBUG/dalvikvm(709): VM cleaning up
12-02 03:10:49.990: ERROR/AndroidRuntime(709): ERROR: thread attach failed
12-02 03:10:50.030: DEBUG/dalvikvm(709): LinearAlloc 0x0 used 639500 of 5242880 (12%)
12-02 03:10:50.230: DEBUG/ddm-heap(715): Got feature list request
12-02 03:10:50.910: INFO/ActivityManager(53): Displayed activity com.droidnova.android.samples/.SelectContact: 1000 ms (total 1000 ms)
12-02 03:10:51.921: INFO/ActivityManager(53): Starting activity: Intent { act=android.intent.action.PICK cmp=com.droidnova.android.samples/.Contacts }
12-02 03:10:52.441: INFO/ActivityManager(53): Displayed activity com.droidnova.android.samples/.Contacts: 431 ms (total 431 ms)
12-02 03:10:53.521: DEBUG/Selection(715): index:0;key:0;value:true;_id
12-02 03:10:54.391: DEBUG/Selection(715): index:0;key:0;value:true;_id
12-02 03:10:54.391: DEBUG/Selection(715): index:1;key:1;value:true;name
12-02 03:10:54.841: DEBUG/Selection(715): index:0;key:0;value:true;_id
12-02 03:10:54.851: DEBUG/Selection(715): index:1;key:1;value:true;name
12-02 03:10:54.851: DEBUG/Selection(715): index:2;key:2;value:true;number
12-02 03:10:57.531: DEBUG/dalvikvm(220): GC freed 43 objects / 2024 bytes in 77ms
12-02 03:11:24.040: DEBUG/Selection(715): index:0;key:0;val开发者_JAVA百科ue:false;_id
12-02 03:11:24.040: DEBUG/Selection(715): index:1;key:1;value:true;name
12-02 03:11:24.050: DEBUG/Selection(715): index:2;key:2;value:true;number
12-02 03:11:25.120: DEBUG/Selection(715): index:0;key:0;value:false;_id
12-02 03:11:25.120: DEBUG/Selection(715): index:1;key:1;value:false;name
12-02 03:11:25.130: DEBUG/Selection(715): index:2;key:2;value:true;number
12-02 03:11:25.670: DEBUG/Selection(715): index:0;key:0;value:false;_id
12-02 03:11:25.680: DEBUG/Selection(715): index:1;key:1;value:false;name
12-02 03:11:25.680: DEBUG/Selection(715): index:2;key:2;value:false;number
12-02 03:11:26.641: DEBUG/Selection(715): index:0;key:0;value:true;_id
12-02 03:11:26.641: DEBUG/Selection(715): index:1;key:1;value:false;name
12-02 03:11:26.651: DEBUG/Selection(715): index:2;key:2;value:false;number
12-02 03:11:27.171: DEBUG/Selection(715): index:0;key:0;value:true;_id
12-02 03:11:27.181: DEBUG/Selection(715): index:1;key:1;value:true;name
12-02 03:11:27.181: DEBUG/Selection(715): index:2;key:2;value:false;number
12-02 03:49:00.191: DEBUG/dalvikvm(53): GC freed 17933 objects / 880976 bytes in 150ms
12-02 04:22:34.131: DEBUG/dalvikvm(102): GC freed 11675 objects / 524064 bytes in 81ms
12-02 04:38:00.181: DEBUG/dalvikvm(53): GC freed 20167 objects / 943152 bytes in 147ms
try this..i am using this code for get Email and Name from Contact. use this code as per your Requirement
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode==CONTACT_PICKER_EMAIL)
{
Cursor cursor = null;
String email = "";
try
{
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI,
null, Email.CONTACT_ID + "=?", new String[] { id },
null);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst())
{
email = cursor.getString(emailIdx);
txtconfirmBy.setText(email);
}
else
{
Log.w(DEBUG_TAG, "No results");
}
}
catch (Exception e)
{
Log.e(DEBUG_TAG, "Failed to get email data", e);
}
finally
{
if (cursor != null)
{
cursor.close();
}
}
}
else if (requestCode==CONTACT_PICKER_NAME)
{
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst())
{
String name = c.getString(c.getColumnIndexOrThrow(People.DISPLAY_NAME));
txtparticipant.setText(name);
}
}
}
}
Send Request for get Details like this
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, CONTACT_PICKER_EMAIL);
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,CONTACT_PICKER_NAME);
精彩评论