我正在Android上通过点击ExpandableListView
中的按钮从电话联系人列表中选择联系人。我的代码在BaseExpandableListAdapter
中。
我可以打开联系人列表,但是选择后我无法在onActivityResult
中得到结果。请帮我。这是我的代码。
public class expandableListAdaptor extends BaseExpandableListAdapter {
private static final int RESULT_PICK_CONTACT =1;
private TextView phone;
private ImageButton btngetContact;
private Context context;
public expandableListAdaptor(Context context, List<String> listTitle, HashMap<String, PackageName> expandbleListDetailes) {
this.context = context;
ListTitle = listTitle;
this.expandbleListDetailes = expandbleListDetailes;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final PackageName packageName = (PackageName) getChild(groupPosition, childPosition);
if(convertView==null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_view_2nd, null);
}
btngetContact = convertView.findViewById(R.id.btnCallphoneNumber);
phone = convertView.findViewById(R.id.etphoneNumber);
btngetContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
((Activity)context). startActivityForResult(intent1, RESULT_PICK_CONTACT);
}
});
return convertView;
}
这是我的onActivityResult代码。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_PICK_CONTACT:
getSelectedContact (data);
break;
}
}
private void getSelectedContact(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null;
Uri uri = data.getData ();
cursor = ((Activity)context).getContentResolver ().query (uri, null, null,null,null);
cursor.moveToFirst ();
int phoneIndex = cursor.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNo = cursor.getString (phoneIndex);
phone.setText (phoneNo);
} catch (Exception e) {
e.printStackTrace ();
}
}