我的手机通讯录中有两个数据,但是RecyclerView在屏幕上只显示了一个数据。
不知道这里是什么问题,不是notifyDataChanged()问题起作用?
Img of Contacts in phone
Img of ContactsTest in phone
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/contacts_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
contact_list.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/contacts_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
AndroidManifest.xml
...
<uses-permission android:name="android.permission.READ_CONTACTS"/>
...
MainActivity.java
package com.example.contactstest;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
List<String> contactsList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView=(RecyclerView) findViewById(R.id.contacts_view);
mRecyclerView.setHasFixedSize(true);
layoutManager =new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
adapter=new MyAdapter(contactsList);
mRecyclerView.setAdapter(adapter);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.
permission.READ_CONTACTS},1);
}else{
readContacts();
}
}
private void readContacts(){
Cursor cursor=null;
try{
cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null,null);
if(cursor!=null){
while(cursor.moveToNext()){
String displayName=cursor.getString(cursor.getColumnIndex(ContactsContract.
CommonDataKinds.Phone.DISPLAY_NAME));
String number=cursor.getString(cursor.getColumnIndex(ContactsContract.
CommonDataKinds.Phone.NUMBER));
contactsList.add(displayName+"\n"+number);
}
adapter.notifyDataSetChanged();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(cursor!= null){
cursor.close();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[]permissions, int[] grantResults){
switch (requestCode){
case 1:
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
readContacts();
}else{
Toast.makeText(this, "You denied the permission", Toast.
LENGTH_SHORT).show();
}
break;
}
}
}
MyAdapter.java
package com.example.contactstest;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mContactsList;
public static class ViewHolder extends RecyclerView.ViewHolder{
private View contactsView;
private TextView contactsName;
public ViewHolder(View view){
super(view);
contactsView=view;
contactsName=(TextView) view.findViewById(R.id.contacts_list);
}
}
public MyAdapter(List<String> list){
mContactsList=list;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_list,parent
,false);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position){
String name=(String) mContactsList.get(position);
holder.contactsName.setText(name);
}
@Override
public int getItemCount(){
return mContactsList.size();
}
}
参考方案
在中将LinearLayout
的高价更改为 android:layout_height="wrap_content"
contact_list.xml
示例代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/contacts_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Android:如何为wrap_content设置高度动画? - java我需要使用ValueAnimator来使用户拖动特定视图时出现自定义“放置字段”。 (我想将字段从gone, height = 0更改为visible, height = wrap_content)。我已经尝试过以下问题的解决方案:How to animate to wrap_content?当我在单个TextView上使用它时,答案就起作用了,但是当我尝试…
不需要选择回收站查看项目 - javaviewHolder.optiontxt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewHolder.optiontxt1.setCompoundDrawablesWithIntrinsicBounds(R.dra…
从服务器加载XML布局(android) - java我对此问题进行了大量研究,但没有得到想要的答案。所以我确实有一个应用程序从服务器获取字符串。字符串系列采用XML格式。这是我将从服务器获得的示例(如您所见,它是一个布局):<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to…
为什么我的应用在启动时总是崩溃-Android - java刚刚遵循了有关如何制作按钮和活动的youtube指南。我按照他的代码减小字体大小,并且在启动时一直崩溃。有人知道为什么吗?public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { su…
android-android studio模拟器中的SSL问题,在手机上可以正常工作 - java我有一个可以通过https调用网络服务的应用程序。当我在手机上运行apk时,效果很好。但是,在模拟器中,所有通过POST的SSL请求均失败,并显示以下信息: 读取错误:ssl = 0xb402be00:SSL库失败,通常是一个协议 错误 错误:100c50bf:SSL例程:ssl3_read_bytes:NO_RENEGOTIATION(外部/无聊的sl /…