gpt4 book ai didi

java - 无法刷新 RecyclerView

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:55:50 24 4
gpt4 key购买 nike

我的手机通讯录中有两个数据,但 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>

联系人列表.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"/>
...

主 Activity .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;
}
}

}

我的适配器.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();
}
}

最佳答案

contact_list.xml 中将 LinearLayout 的高度更改为 android:layout_height="wrap_content"

示例代码

<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>

关于java - 无法刷新 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51130002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com