我是应用程序开发的新手,所以我写了一个代码,我想从电话簿中读取我的联系人列表,并设法点击该联系人。现在的问题是,当我点击联系人listView上显示的联系人时,我想在另一个页面上显示联系人姓名和电话号码。我想在add_friend类上显示姓名和电话号码。
这是contact.java
public class Contact extends AppCompatActivity implements LoaderManager
.LoaderCallbacks<Cursor> {
private static final String[] FROM_COLUMNS = {
ContactsContract.Data.CONTACT_ID,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Data.PHOTO_ID,
};
private static final int REQUEST_PERMISSION = 0;private ContactAdapter mContactAdapter;
private RecyclerView mContactRecyclerView;
private static final int LOADER_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
if (ActivityCompat.checkSelfPermission(Contact.this, Manifest.permission
.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
Contact.this,
new String[]{
Manifest.permission.READ_CONTACTS
},
REQUEST_PERMISSION
);
} else {
getSupportLoaderManager().initLoader(LOADER_ID, savedInstanceState, this);
}
//Initialising LoaderManger
getSupportLoaderManager().initLoader(LOADER_ID, savedInstanceState, this);
mContactRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_contacts);
mContactRecyclerView.setHasFixedSize(true);
mContactRecyclerView.setLayoutManager(new LinearLayoutManager(Contact.this));
mContactAdapter = new ContactAdapter(Contact.this, null, ContactsContract.Data.CONTACT_ID);
mContactRecyclerView.setAdapter(mContactAdapter);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case LOADER_ID:
return new CursorLoader(Contact.this, ContactsContract.Data.CONTENT_URI, FROM_COLUMNS, null, null,
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME) + " ASC");
default:
if (BuildConfig.DEBUG)
throw new IllegalArgumentException("no id handled");
return null;
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mContactAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mContactAdapter.swapCursor(null);
}
}
这是contactADAPTER
public class ContactAdapter extends CursorRecyclerViewAdapter<ContactAdapter.ContactViewHolder> {
public ContactAdapter(Context context, Cursor cursor, String id){
super(context, cursor, id);
}TextView contact_name;
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_contact,
parent, false);
contact_name=(TextView)view.findViewById(R.id.contact_display_name);
contact_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent contIntent= new Intent(mContext.getApplicationContext(), Add_Friend.class );
contIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
mContext.getApplicationContext().startActivity(contIntent);
}
});
return new ContactViewHolder(view);
}
@Override
public void onBindViewHolder(ContactViewHolder viewHolder, Cursor cursor) {
//Now we can handle onBindViewHolder
long contactId = getItemId(cursor.getPosition());
//Setting the username
String username = cursor.getString(cursor.getColumnIndex(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME
));
viewHolder.contactDisplayNameTextView.setText(username);
//Setting the photo
long photoId = cursor.getLong(cursor.getColumnIndex(
ContactsContract.Data.PHOTO_ID
));
if (photoId != 0) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo
.CONTENT_DIRECTORY);
viewHolder.contactDisplayImageView.setImageURI(photoUri);
} else {
viewHolder.contactDisplayImageView.setImageResource(R.drawable.prof_icon);
}
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
ImageView contactDisplayImageView;
TextView contactDisplayNameTextView;
public ContactViewHolder(View itemView) {
super(itemView);
contactDisplayImageView = (ImageView) itemView.findViewById(R.id.contact_display);
contactDisplayNameTextView = (TextView) itemView.findViewById(R.id.contact_display_name);
}
}
}
这是回收者的观点
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
@SuppressWarnings("unused")
private static final String TAG = CursorRecyclerViewAdapter.class.getSimpleName();
protected Context mContext;
private Cursor mCursor;
private boolean mDataValid;
private int mRowIdColumn;
private String mId;
private DataSetObserver mDataSetObserver;
public CursorRecyclerViewAdapter(Context context, Cursor cursor, String id) {
mContext = context;
mCursor = cursor;
mDataValid = cursor != null;
mRowIdColumn = mDataValid ? mCursor.getColumnIndex(id) :-1;
mId = id;
mDataSetObserver = new NotifyingDataSetObserver(this);
if (mCursor != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
}
@SuppressWarnings("unused")
protected Cursor getCursor() {
return mCursor;
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(VH holder, int position) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
onBindViewHolder(holder, mCursor);
}
@Override
public int getItemCount() {
if (mDataValid && mCursor != null) {
return mCursor.getCount();
}
return 0;
}
@Override
public long getItemId(int position) {
if (mDataValid && mCursor != null && mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIdColumn);
}
return 0;
}
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(true);
}
public abstract void onBindViewHolder(VH viewHolder, Cursor cursor);
@SuppressWarnings("unused")
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
final Cursor oldCursor = mCursor;
if (oldCursor != null && mDataSetObserver != null) {
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
mCursor = newCursor;
if (mCursor != null) {
if (mDataSetObserver != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
mRowIdColumn = newCursor.getColumnIndexOrThrow(mId);
mDataValid = true;
notifyDataSetChanged();
} else {
mRowIdColumn = -1;
mDataValid = false;
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
return oldCursor;
}
public void setDataValid(boolean dataValid) {
mDataValid = dataValid;
}
private class NotifyingDataSetObserver extends DataSetObserver {
private RecyclerView.Adapter adapter;
public NotifyingDataSetObserver(RecyclerView.Adapter adapter) {
this.adapter = adapter;
}
@Override
public void onChanged() {
super.onChanged();
((CursorRecyclerViewAdapter) adapter).setDataValid(true);
adapter.notifyDataSetChanged();
}
@Override
public void onInvalidated() {
super.onInvalidated();
((CursorRecyclerViewAdapter) adapter).setDataValid(false);
}
}
这是我想要显示所选联系人的姓名和号码的地方
公共类Add_Friend扩展了AppCompatActivity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__friend);
}
}
这是我想要显示姓名和电话号码的布局
' <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xml :android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_height="wrap_content">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/contact_display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Contact Username"
android:textColor="@android:color/black"
android:textSize="20sp"
android:layout_marginTop="26dp"
android:layout_below="@+id/contact_display"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="0986857456"
android:textColor="@android:color/black"
android:textSize="20sp"
android:layout_below="@+id/contact_display_name"
android:layout_alignLeft="@+id/contact_display_name"
android:layout_alignStart="@+id/contact_display_name"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add friend"
android:layout_below="@+id/contact_display_name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/contact_display"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/prof_icon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
这就是我希望它出现的方式 this is how I want it to appear
答案 0 :(得分:0)
Intent contIntent= new Intent(mContext.getApplicationContext(), Add_Friend.class );
在此行之后输入您的数据:
contIntent.putExtra("name", data1);
contIntent.putExtra("number", data2);
并从onCreate
活动获取Add_Friend
中的数据,如下所示:
String name = getIntent().getStringExtra("name");
String number = getIntent().getStringExtra("number");