public class BlankFragment3 extends Fragment {
ArrayList<genreInfo> genreList = new ArrayList<>();
RecyclerView recyclerView2;
private genreAdapter genreAdapter1;
String id;
Cursor cursor;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public BlankFragment3() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.genrere, container, false);
recyclerView2 = rootView. findViewById(R.id.recyclerView2);
genreAdapter1 = new genreAdapter(genreList, getActivity());
recyclerView2.setAdapter(genreAdapter1);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView2.getContext(),
linearLayoutManager.getOrientation());
recyclerView2.setLayoutManager(linearLayoutManager);
recyclerView2.addItemDecoration(dividerItemDecoration);
getGenreList();
return rootView;
}
public void getGenreList() {
String[] proj1 = new String[]{
MediaStore.Audio.Genres.NAME,
MediaStore.Audio.Genres._ID
};
ContentResolver cr = getContext().getContentResolver();
cursor = cr.query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, proj1, null, null, null);
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
int index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);
String genre = cursor.getString(index);
index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);
long genreId = Long.parseLong(cursor.getString(index));
Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);
Cursor tempCursor = cr.query(uri, null, null, null, null);
if (tempCursor.moveToFirst()) {
while (tempCursor.moveToNext()) {
index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
String title = tempCursor.getString(index);
index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
String artist = tempCursor.getString(index);
index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
String album = tempCursor.getString(index);
genreInfo g = new genreInfo(artist, title, album, genre);
genreList.add(g);
}
tempCursor.close();
}
}
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
genreAdapter
public class genreAdapter extends RecyclerView.Adapter<genreAdapter.GenreHolder> {
ArrayList<genreInfo> genreList=new ArrayList<>();
Context context;
MediaMetadataRetriever metaRetriver;
byte[] art;
public genreAdapter(ArrayList<genreInfo> genreList, Context context) {
this.genreList = genreList;
this.context = context;
}
@Override
public genreAdapter.GenreHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View myView = LayoutInflater.from(context).inflate(R.layout.genre,parent,false);
return new GenreHolder(myView);
}
@Override
public void onBindViewHolder(GenreHolder holder, int position) {
final genreInfo a = genreList.get(position);
holder.album.setText(genreList.get(position).getAlbum());
holder.title.setText(genreList.get(position).getTitle());
holder.artist.setText(genreList.get(position).getArtist());
/*
holder.artist.setText(genreList.get(position).getdName());
holder.album_art.setImageDrawable(Drawable.createFromPath(String.valueOf(albumList.get(position).getAlbumImg())));
*/
/* metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(String.valueOf(genreList.get(position).getID()));
try {
art = metaRetriver.getEmbeddedPicture();
Bitmap songImage = BitmapFactory.decodeByteArray(art,0,art.length);
holder.album_art.setImageBitmap(songImage);
} catch (Exception e)
{
holder.album_art.setBackgroundColor(Color.GRAY);
}*/
}
@Override
public int getItemCount()
{
return genreList.size();
}
public class GenreHolder extends RecyclerView.ViewHolder {
public TextView album;
public TextView artist;
public TextView title;
// public ImageView album_art;
public GenreHolder(View itemView) {
super(itemView);
// album_art = (ImageView) itemView.findViewById(R.id.albumArt);
artist = (TextView) itemView.findViewById(R.id.artistName);
title = (TextView) itemView.findViewById(R.id.titleName);
album = (TextView) itemView.findViewById(R.id.songName);
}
}
}
答案 0 :(得分:0)
您在创建适配器后填充ArrayList
,但当时ArrayList
为空,
在创建适配器之前调用getGenreList();
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.genrere, container, false);
recyclerView2 = rootView. findViewById(R.id.recyclerView2);
getGenreList();
genreAdapter1 = new genreAdapter(genreList, getActivity());
recyclerView2.setAdapter(genreAdapter1);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView2.getContext(),
linearLayoutManager.getOrientation());
recyclerView2.setLayoutManager(linearLayoutManager);
recyclerView2.addItemDecoration(dividerItemDecoration);
return rootView;
}