Android:RecyclerView适配器不';权限检查后不能加载数据

Android:RecyclerView适配器不';权限检查后不能加载数据,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,RecyclerView适配器在权限检查后首次启动时不加载数据。我必须重新点击选项卡以获取数据 我已经访问/尝试了这些链接- -- -- 这是密码- onCreateView- @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_thir

RecyclerView适配器在权限检查后首次启动时不加载数据。我必须重新点击选项卡以获取数据

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

--

--

这是密码-

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;
}
loadAudio() //  call to method if permission granted
private void loadAudio() {
ContentResolver contentResolver = getActivity().getContentResolver();
... /** accessing contents */ ...

   if (cursor != null) {
        cursor.close();
      }
  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);
}
}
@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();
                    }
                }
            }
        }
    }

}
onRequestPermissionResult-

@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;
}
loadAudio() //  call to method if permission granted
private void loadAudio() {
ContentResolver contentResolver = getActivity().getContentResolver();
... /** accessing contents */ ...

   if (cursor != null) {
        cursor.close();
      }
  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);
}
}
@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();
                    }
                }
            }
        }
    }

}
loadAudio()-

@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;
}
loadAudio() //  call to method if permission granted
private void loadAudio() {
ContentResolver contentResolver = getActivity().getContentResolver();
... /** accessing contents */ ...

   if (cursor != null) {
        cursor.close();
      }
  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);
}
}
@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();
                    }
                }
            }
        }
    }

}
initRecyclerView-

@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;
}
loadAudio() //  call to method if permission granted
private void loadAudio() {
ContentResolver contentResolver = getActivity().getContentResolver();
... /** accessing contents */ ...

   if (cursor != null) {
        cursor.close();
      }
  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);
}
}
@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();
                    }
                }
            }
        }
    }

}
onRequestPermissionResult-

@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;
}
loadAudio() //  call to method if permission granted
private void loadAudio() {
ContentResolver contentResolver = getActivity().getContentResolver();
... /** accessing contents */ ...

   if (cursor != null) {
        cursor.close();
      }
  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);
}
}
@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();
                    }
                }
            }
        }
    }

}
@覆盖
公共无效onRequestPermissionsResult(int requestCode,
字符串权限[],int[]grantResults){
String TAG=“LOG\u权限”;
Log.d(标记“权限回调调用----------------”);
开关(请求代码){
案例请求\u ID\u多个\u权限:{
Map perms=new HashMap();
//使用这两个权限初始化映射
perms.put(Manifest.permission.READ_PHONE_状态,PackageManager.permission_已授予);
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE,PackageManager.permission_grated);
//填写用户的实际结果
如果(grantResults.length>0){
for(int i=0;i
我不知道怎么解决这个问题。请,任何帮助都将不胜感激。 我已经在这里问过这个问题了,但是没有用


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

最后 这对我有帮助

@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);
    }
}
虽然它们最初用于嵌套片段,但它也适用于顶级片段。 如果其他人也有同样的问题,请检查


请访问上面的链接以了解其工作原理。

您是否尝试过调试MaudoList中的数据?如果您请求权限,您需要在活动中处理回调
public void on requestpermissions result
,我假设如果您在授予权限后重新启动应用程序,如果
loadAudio()
正确,将按预期工作。@Pavan是的!但只有在点击第二个标签后!不是在第一次单击选项卡期间。但第一次之后,它的工作非常完美!第一次尝试时出现了什么错误日志mark提到你在哪里请求权限没有错误日志!甚至initRecyclerView内部的sysout也没有执行。