Ich entwickle eine Musik-App. Folgendes ist der Code meiner Liederliste. Wenn ich meine App starte, werden nur Musikgrafiken angezeigt. Andere Informationen wie Albumname und Dauer werden nicht in ListView
angezeigt.Listenelement ist in Fragment nicht sichtbar
public class songs_list extends Fragment {
// Adapter that exposes data from a Cursor to a ListView widget.
private songs_list.MediaCursorAdapter mediaAdapter = null;
private String currentFile="";
private boolean isStarted = true;
private TextView selelctedFile = null;
private View v=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Use cursor to get data from external resource
Cursor cursor = getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
// check Cursor has data or not
if (null != cursor) {
cursor.moveToFirst();
mediaAdapter = new MediaCursorAdapter(getActivity().getApplicationContext(), R.layout.list_layout, cursor);
v=inflater.inflate(R.layout.fragment_songs_list, container, false);
// TextView title = (TextView) view.findViewById(R.id.title);
ListView lv = (ListView)v.findViewById(android.R.id.list);
lv.setAdapter(mediaAdapter);
}
// Inflate the layout for this fragment
return v;
}
MediaPlayer player = new MediaPlayer();
//MediaCursorAdapter class construction (what to show)
private class MediaCursorAdapter extends SimpleCursorAdapter {
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[]{MediaStore.Audio.Albums.ALBUM_ID, MediaStore.MediaColumns.TITLE, MediaStore.EXTRA_MEDIA_ARTIST, MediaStore.Audio.AudioColumns.DURATION},
new int[]{R.id.icon, R.id.title, R.id.artistname, R.id.duration});
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
ImageView img = (ImageView) view.findViewById(R.id.icon);
TextView name = (TextView) view.findViewById(R.id.artistname);
TextView duration = (TextView) view.findViewById(R.id.duration);
// icon
long albumid = Long.parseLong((cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID))));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
ContentResolver res = context.getContentResolver();
InputStream in = null;
try {
in = res.openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap artwork = BitmapFactory.decodeStream(in);
img.setImageBitmap(artwork);
title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.EXTRA_MEDIA_ARTIST)));
long durationInMs = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
double durationInMin = ((double) durationInMs/1000.0)/60.0;
durationInMin = new BigDecimal(Double.toString(durationInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue();
duration.setText("" + durationInMin);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.list_layout, parent, false);
bindView(v, context, cursor);
return v;
}
}
}
ist ListItem Text weiß gefärbt? – rupinderjeet
ja das war das Problem. aber ich bin immer noch verwirrt wie Standardfarbe ist schwarz nicht weiß. –
ist es wegen Ihres Themas – HAXM