我从手机中检索了联系人,并选择了多个联系人。我需要将选定的联系人发送到android studio.i中的下一个类中,试图发送该数据,但它显示了一个空白页面。如何将选定的联系人显示到下一页。我的主要活动是: package com.example.vijayasrivudanti.contact;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ContactDetails extends AppCompatActivity implements View.OnClickListener {
ListView mainListView;
Contact[] contact_read;
Cursor mCursor;
ArrayAdapter<Contact> listAdapter;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_details);
b=(Button)findViewById(R.id.done);
b.setOnClickListener(this);
mainListView = (ListView) findViewById(R.id.listview);
mainListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
Contact con = listAdapter.getItem(position);
con.toggleChecked();
ContactViewHolder viewHolder = (ContactViewHolder) item
.getTag();
viewHolder.getCheckBox().setChecked(con.isChecked());
}
});
String[] projection = new String[] { ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection,
ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
ContactsContract.Contacts.DISPLAY_NAME);
if (mCursor != null) {
mCursor.moveToFirst();
contact_read = new Contact[mCursor.getCount()];
// Add Contacts to the Array
int j = 0;
do {
contact_read[j] = new Contact(mCursor.getString(mCursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
j++;
} while (mCursor.moveToNext());
} else {
System.out.println("Cursor is NULL");
}
// Add Contact Class to the Arraylist
ArrayList<Contact> contactList = new ArrayList<Contact>();
contactList.addAll(Arrays.asList(contact_read));
// Set our custom array adapter as the ListView's adapter.
//listAdapter = new ArrayAdapter<Contact>(this,android.R.layout.simple_list_item_multiple_choice,contactList);
listAdapter = new ContactArrayAdapter(this,contactList);
mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mainListView.setAdapter(listAdapter);
}
@Override
public void onClick(View view) {
SparseBooleanArray checked = mainListView.getCheckedItemPositions();
ArrayList<Contact> selectedContacts = new ArrayList<Contact>();
for(int i = 0;i < checked.size();i++){
int position = checked.keyAt(i);
if(checked.valueAt(i))
selectedContacts .add(listAdapter.getItem(position));
}
String[] outputStrArr = new String[selectedContacts.size()];
for (int i =0;i < selectedContacts.size();i++)
{
outputStrArr[i] = String.valueOf(selectedContacts.get(i));
}
Intent i = new Intent(ContactDetails.this,Display.class);
Bundle b = new Bundle();
b.putStringArray("selectedContacts",outputStrArr);
i.putExtras(b);
startActivity(i);
Toast.makeText(this,"selectedcontacts",Toast.LENGTH_LONG).show();
}
/** Holds Contact data. */
@SuppressWarnings("unused")
public static class Contact {
private String name = "";
private boolean checked = false;
public Contact() {
}
public Contact(String name) {
this.name = name;
}
public Contact(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name;
}
public void toggleChecked() {
checked = !checked;
}
}
/** Holds child views for one row. */
@SuppressWarnings("unused")
private static class ContactViewHolder {
private CheckBox checkBox;
private TextView textView;
public ContactViewHolder() {
}
public ContactViewHolder(TextView textView, CheckBox checkBox) {
this.checkBox = checkBox;
this.textView = textView;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
/** Custom adapter for displaying an array of Contact objects. */
private static class ContactArrayAdapter extends ArrayAdapter<Contact> {
private LayoutInflater inflater;
public ContactArrayAdapter(Context context, List<Contact> contactList) {
super(context, R.layout.simplerow, R.id.rowTextView, contactList);
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Contact to display
Contact cont = (Contact) this.getItem(position);
System.out.println(String.valueOf(position));
// The child views in each row.
CheckBox checkBox;
TextView textView;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, null);
// Find the child views.
textView = (TextView) convertView
.findViewById(R.id.rowTextView);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
// Optimization: Tag the row with it's child views, so we don't
// have to
// call findViewById() later when we reuse the row.
convertView.setTag(new ContactViewHolder(textView, checkBox));
// If CheckBox is toggled, update the Contact it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Contact contact = (Contact) cb.getTag();
contact.setChecked(cb.isChecked());
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call
// findViewById().
ContactViewHolder viewHolder = (ContactViewHolder) convertView
.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
// Tag the CheckBox with the Contact it is displaying, so that we
// can
// access the Contact in onClick() when the CheckBox is toggled.
checkBox.setTag(cont);
// Display Contact data
checkBox.setChecked(cont.isChecked());
textView.setText(cont.getName());
return convertView;
}
}
}
我显示所选联系人的下一个布局是:
public class Display extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedContacts");
ListView lv = (ListView)findViewById(R.id.outputList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,resultArr);
lv.setAdapter(adapter);
}
}