权限检查后,RecyclerView适配器在首次启动时不会加载数据。我必须重新点击选项卡以获取数据。
我已经访问/尝试了这些链接-
-RecyclerView doesn't load data in first launch using FirebaseRecyclerAdapter
-RecyclerView doesn't load items on first start of activity
-recyclerview not loading adapter values in a fragment
但是什么都没有。
试图设置全局适配器,所以我可以从onResume()调用notifyDataSetChanged()。仍然没有用。
这是代码-
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()) {
loadSongList();
}
return view;
}
onRequestPermissionResult-
loadSongList(); // method
loadSongList()-
private void loadSongList() {
loadAudio();
initRecyclerView();
}
loadAudio()-
loadAudio() // accessing data from content resolver
initRecyclerView()-
private void initRecyclerView() {
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);
recyclerView.setAdapter(adapter);
RecyclerViewItemClick.addTo(recyclerView).setOnItemClickListener(new RecyclerViewItemClick.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// life sucks
}
});
}
}
编辑:
loadAudio()-
private void loadAudio() {
ContentResolver contentResolver = getActivity().getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
if (cursor != null && cursor.getCount() > 0) {
mAudioList = new ArrayList<>();
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
Long albumId = cursor.getLong(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
Long artWorkId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
int duration = cursor.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, artWorkId);
// Save to audioList
mAudioList.add(new Audio(albumId, data, title, album, artist, albumArtUri, duration));
}
}
if (cursor != null) {
cursor.close();
}
}
音频适配器-
public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.CustomViewHolder> {
List<Audio> mList = Collections.emptyList();
Context mContext;
public AudioAdapter(List<Audio> list, Context context) {
mList = list;
mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_row, parent, false);
return new CustomViewHolder(v);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Audio audio = mList.get(position);
holder.title.setText(audio.getTitle());
holder.description.setText(audio.getArtist());
Glide.with(mContext).load(audio.imageId).asBitmap()
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.skipMemoryCache(true)
.placeholder(R.drawable.headset_2)
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mList.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void insert(int position, Audio list) {
mList.add(position, list);
notifyItemChanged(position);
}
public void remove(Audio list) {
int position = mList.indexOf(list);
mList.remove(position);
notifyItemRemoved(position);
}
static class CustomViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
TextView description;
ImageView imageView;
public CustomViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
title = (TextView) itemView.findViewById(R.id.playList_name);
description = (TextView) itemView.findViewById(R.id.album_artist);
imageView = (ImageView) itemView.findViewById(R.id.imageView1);
}
}}
UDPATE:
checkAndRequestPermission-
private boolean checkAndRequestPermissions() {
if (SDK_INT >= Build.VERSION_CODES.M) {
int permissionReadPhoneState = ContextCompat.checkSelfPermission(getContext(), (Manifest.permission.READ_PHONE_STATE));
int permissionStorage = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionReadPhoneState != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
}
if (permissionStorage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
requestPermissions(listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
} else {
return true;
}
}
return false;
}
在删除ActivityCompat并仅调用requestPermission之后,该片段正在接收回调,但是现在我遇到了错误。下面的logcat-
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at com.example.ansh.sharedpreferencessimple.service.MusicService.playSong(MusicService.java:167)
at com.example.ansh.sharedpreferencessimple.ThirdFragment$2.onItemClicked(ThirdFragment.java:196)
at com.example.ansh.sharedpreferencessimple.helper.RecyclerViewItemClick$1.onClick(RecyclerViewItemClick.java:21)
at android.view.View.performClick(View.java:6205)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
任何帮助,将不胜感激,谢谢。
最佳答案
这是您的问题的答案。
您的loadAudio()方法会填满您的mAudioList,它是您的数组列表或列出了所有内容。
因此,主要问题是,当您调用loadAudio()然后在initRecyclerView()中调用initRecyclerView()时,mAudioList仍然为null,因为那时您的mAudioList尚未在loadAudio()中填充。
因此,您的答案是在loadAudio()函数末尾调用initRecyclerView(),这将解决您的问题。
对于
private void loadAudio()
{
// Get your arraylist of audio and all the stuff
if (cursor != null)
{
cursor.close();
}
// Call init function here.
initRecyclerView();
}