我是新的应用程序开发人员,所以我写了一个代码,希望从通讯录中读取联系人列表,并成功点击该联系人。现在的问题是,当单击“我的联系人”列表视图中显示的联系人时,我想在不同的页面上显示联系人的姓名和电话号码。我想在add_friend类中显示姓名和电话号码。
这是联系人。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扩展AppCompative活动{
@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>
这就是我想要它出现的样子这就是我想要它出现的样子
Intent contIntent= new Intent(mContext.getApplicationContext(), Add_Friend.class );
在这一行之后把你的数据这样:
contIntent.putExtra("name", data1);
contIntent.putExtra("number", data2);
并从Add_Friend
活动中获取数据,如下所示:
String name = getIntent().getStringExtra("name");
String number = getIntent().getStringExtra("number");
问题内容: 我想在Android中分别阅读所有SIM卡联系人和电话联系人。我进行了搜索,发现很多人对此有疑问,但我找不到任何解决方案。我在这里喜欢一些答案,但这对我不起作用。当我测试它给我谷歌联系人: 但是当我测试时,它并没有给我SIM卡联系人: 然后我发现RawContacts是在此处由同步适配器创建的联系人。那可能是问题所在。有谁能告诉我 所有simcontacts 所有电话联络人 谢谢。 问
问题内容: 我正在尝试根据给定的联系人电话号码检索联系人姓名。我做了一个可以在所有API版本中使用的函数,因为我无法使其在1.6版中运行,而且我也看不到问题所在,也许有人可以发现它? 请注意,我已经为字符串替换了API常量,因此没有过时的警告问题。 问题答案: 使用反射而不是比较sdk版本。
问题内容: 我想获取联系人姓名,但无法。看完这个答案后,我尝试使用family,给定的和显示的来获取名称,但没有任何效果 / 问题答案: 尝试以下代码以获取特定号码的联系方式 更多详细信息请参见下面的链接https://tausiq.wordpress.com/2012/08/23/android-get-contact- details-id-name-phone-photo/
问题内容: 我在我的应用中遇到了三到两次相同的联系人,这种情况发生在某些联系人而不是每个联系人上。在我的应用程序中,一切都按预期工作,但是单击我的“显示联系人”时,显示三个时间相同的联系人,但在手机联系人中仅存储一次。我从我这边尝试了一切,但是无法解决这个问题,请问有什么机构可以帮助我。还是有其他替代方法可以做到这一点。 这是我的代码: 问题答案: 您正在为每个电话的每个联系人打印那些“ Fetc
我想从Android的联系簿中获得电话号码。我尝试了许多方法,但我无法获得电话号码。我有联系人姓名和状态,但当我尝试获取电话号码时,它不起作用。我的代码是。
联系人名单 登录至联系人名单 确认登入状态 编辑联系人名单