我在片段中请求用户许可(由于该片段中的RecyclerView),但是在给出用户许可后,RecyclerView为空。但是,当我重新启动应用程序时,recyclerView会填充数据。
public class RecyclerViewFragment extends Fragment {
public RecyclerView songListRecycleView;
recyclerViewAdapter mAdapter;
ArrayList<songInfo> songs;
public void RecyclerViewFragment(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_view_fragment_layout,container,false);
songs = new ArrayList<>();
if(getActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
requestStoragePermission();
//Toast.makeText(getActivity(), "Permission Granted YES", Toast.LENGTH_SHORT).show();
//songs = getSongArrayList();
//Log.v("listSize",""+songs.size());
}else if (getActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED){
songs = getSongArrayList();
//Toast.makeText(getContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
}
//setting recycler view
songListRecycleView = view.findViewById(R.id.songListViewId);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
songListRecycleView.setLayoutManager(mLayoutManager);
return view;
}
private static final int MY_PERMISSIONS_REQUEST = 100;
private void requestStoragePermission(){
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
songs = getSongArrayList();
Toast.makeText(getContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
// permission was granted, yay! Do the
// file-related task you need to do.
}
else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
mAdapter = new recyclerViewAdapter(getActivity(),songs);
songListRecycleView.setAdapter(mAdapter);
}
public ArrayList<songInfo> getSongArrayList(){
ArrayList<songInfo> songInfos = new ArrayList<songInfo>();
Cursor cursor = getSongCursor();
if(cursor!=null){
Log.v("cursor_NULL","NO");
Toast.makeText(getActivity(), ""+cursor.getCount(), Toast.LENGTH_SHORT).show();
int titleIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int dataIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
songInfo sInfo;
cursor.moveToFirst();
for(int i=0;i<cursor.getCount();i++){
sInfo = new songInfo((String)cursor.getString(titleIndex),(String)cursor.getString(dataIndex));
songInfos.add(sInfo);
cursor.moveToNext();
}
}else{
Log.v("cursor_NULL","YES");
}
return songInfos;
}
public Cursor getSongCursor(){
try{
Uri externalUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = new String[]{
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA};
String sortOrder = MediaStore.Audio.Media.TITLE+" ASC";
Cursor songCursor = getContext().getContentResolver().query(externalUri,projection,null,null,sortOrder);
Log.v("cursor_length","" + songCursor.getCount());
return songCursor;
}catch (Exception e){
return null;
}
}
}
而且我也想知道,首先将内容放入游标,然后将数据插入ArrayList,然后将其用于RecyclerView Adapter是个好主意。如果我直接在Adapter中使用游标,或者在Adapater中使用ArrayList会有什么性能上的区别。