Reputation: 43
I have two data in my phone contacts.Yet, only one was shown in screen by RecyclerView.
Don't know what problem here, is it the problem of notifyDataChanged() not working?
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();
}
}
Upvotes: 4
Views: 76
Reputation: 69734
Change hight of LinearLayout
to android:layout_height="wrap_content"
in contact_list.xml
SAMPLE CODE
<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>
Upvotes: 4
Reputation:
Change this things..
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_parent">
<TextView
android:id="@+id/contacts_list"
android:layout_width="match_parent"
android:layout_height="wrap_parent" />
</LinearLayout>
and also make some change into adapter class in constuctor..
private Context context;
public IndexItemAdapter(Context context, List<UserData> userVoList) {
this.userVoList = userVoList;
this.context = context;
}
Upvotes: 0