Android:在Permission Check之后,RecyclerView适配器不会加载数据

时间:2017-06-07 16:40:20

标签: android android-fragments android-recyclerview

在Permission检查后,RecyclerView适配器不会在首次启动时加载数据。我必须重新点击标签才能获取数据。

我已经访问过/试过这些链接 -

- RecyclerView doesn't load data in first launch using FirebaseRecyclerAdapter

- RecyclerView doesn't load items on first start of activity

此处的代码 -

onCreateView -

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_third, container, false);

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

if (checkAndRequestPermissions()) {
    loadAudio();
}

return view;
}

onRequestPermissionResult -

loadAudio() //  call to method if permission granted

loadAudio() -

private void loadAudio() {
ContentResolver contentResolver = getActivity().getContentResolver();
... /** accessing contents */ ...

   if (cursor != null) {
        cursor.close();
      }
  initRecyclerView();
 }

initRecyclerView -

private void initRecyclerView() {

// To check if this method called at first time tab click
// which it doesn't.
System.out.println("==== mAudioList ==="+mAudioList+"::");
// ------

if (mAudioList != null && mAudioList.size() > 0) {
    AudioAdapter adapter = new AudioAdapter(mAudioList, getActivity());
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);
    DIviderItemDecoration dividerItemDecoration = new DIviderItemDecoration(getContext(), linearLayoutManager.getOrientation());
    recyclerView.addItemDecoration(dividerItemDecoration);

    // thought this would help
    adapter.notifyDataSetChanged();

    recyclerView.setAdapter(adapter);
}
}

onRequestPermissionResult -

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    String TAG = "LOG_PERMISSION";
    Log.d(TAG, "Permission callback called-------");
    switch (requestCode) {
        case REQUEST_ID_MULTIPLE_PERMISSIONS: {

            Map<String, Integer> perms = new HashMap<>();
            // Initialize the map with both permissions
            perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
            // Fill with actual results from user
            if (grantResults.length > 0) {
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                // Check for both permissions

                if (perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        ) {
                    Log.d(TAG, "Phone state and storage permissions granted");
                    loadAudio();
                } else {
                    Log.d(TAG, "Some permissions are not granted ask again ");
                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) ||
                            ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_PHONE_STATE)) {
                        showDialogOK("Phone state and storage permissions required for this app",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        switch (which) {
                                            case DialogInterface.BUTTON_POSITIVE:
                                                checkAndRequestPermissions();
                                                break;
                                            case DialogInterface.BUTTON_NEGATIVE:
                                                // proceed with logic by disabling the related features or quit the app.
                                                break;
                                        }
                                    }
                                });
                    }
                    else {
                        Toast.makeText(getContext(), "Go to settings and enable permissions", Toast.LENGTH_LONG)
                                .show();
                    }
                }
            }
        }
    }

}

我不知道如何解决这个问题。请,任何帮助将得到赞赏。 已经在这里问过这个问题 - question但是那没有用。

注意:我的其他片段生命周期方法中没有代码。

1 个答案:

答案 0 :(得分:0)

终于!我得到了答案。 问题不在于权限,而在于onRequestPermissionResult,因为它没有得到回调!

answer帮助了我。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(/**fragmentContainer*/);
    if (fragment != null) {
        fragment.onRequestPermissionsResult(requestCode&0xff, permissions, grantResults);
    }
}

虽然他们最初回答了嵌套片段,但它也适用于顶级片段。 如果其他人遇到同样问题,请查看。

请访问以上链接,了解其运作方式。