簡體   English   中英

無法刷新RecyclerView

[英]Can't refresh RecyclerView

我的手機通訊錄中有兩個數據。但是,RecyclerView只在屏幕上顯示了一個數據。

不知道這里有什么問題,是不是notifyDataChanged()無法正常工作的問題?

電話中的聯系人

Img的ContactsTest在手機上

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();
    }
}

改變HIGHT LinearLayoutandroid: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>

改變這個......

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

並在constuctor中對適配器類進行一些更改..

 private Context context;
public IndexItemAdapter(Context context, List<UserData> userVoList) {
    this.userVoList = userVoList;
    this.context = context;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM