我正在尝试为Android操作系统构建音乐播放器。我正在动态创建一个listView,并使用Uri设置标签,以便稍后可以使用getTag对Uris进行检索。但是,这不起作用。它在getTag行给出了ClassCastException。这是我的代码:
SongList.java:
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SongList extends Fragment {
private ArrayList<Song> songList;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_song_list, container, false);
//retrieve list view
ListView songView = (ListView)view.findViewById(R.id.listView);
//instantiate list
songList = new ArrayList<>();
getSongList();
Collections.sort(songList, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
final SongAdapter songAdapter = new SongAdapter( getActivity(), songList);
songView.setAdapter(songAdapter);
songView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setSong(view);
}
});
return view;
}
void getSongList() {
//retrieve song info
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
//iterate through music
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int albumColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM);
int albumIdColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM_ID);
int UriColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisAlbum = musicCursor.getString(albumColumn);
long thisAlbumId = musicCursor.getLong(albumIdColumn);
Uri thisUri = Uri.parse( musicCursor.getString( UriColumn ) );
songList.add(new Song(thisId, thisTitle, thisArtist, thisAlbum, thisAlbumId, thisUri));
}
while (musicCursor.moveToNext());
}
}
public void setSong(View view) {
if ( MainActivity.isCreatedCheck() ){
Toast.makeText(getActivity(), "Already started", Toast.LENGTH_LONG).show();
}else {
Uri songUri = (Uri) view.getTag();
final MediaPlayer mediaPlayer = MediaPlayer.create(getActivity(), songUri);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}
}
}
SongAdapter.java:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class SongAdapter extends BaseAdapter {
//song list and layout
private ArrayList<Song> songs;
private LayoutInflater songInf;
//constructor
public SongAdapter(Context c, ArrayList<Song> theSongs){
songs=theSongs;
songInf=LayoutInflater.from(c);
}
@Override
public int getCount() {
return songs.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//map to song layout
RelativeLayout songLay = (RelativeLayout) songInf.inflate(R.layout.song_list_item, parent, false);
//get title and artist views
TextView songView = (TextView)songLay.findViewById(R.id.titleListTextView);
TextView artistView = (TextView)songLay.findViewById(R.id.artistListTextView);
RelativeLayout relativeLayout = (RelativeLayout)songLay.findViewById(R.id.layoutSelector);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
relativeLayout.setTag( currSong.getURI() );
//set position as tag
songLay.setTag(position);
return songLay;
}
}
song_list_item.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:background="@color/gray_200"
android:layout_marginBottom="@dimen/bottom_song_border"
android:layout_height="@dimen/song_height"
android:id="@+id/layoutSelector">
<com.makeramen.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:src="@drawable/album_default"
android:id="@+id/albumListImageView"
android:layout_width="@dimen/song_album_metrics"
android:layout_height="@dimen/song_album_metrics"
app:riv_corner_radius="30dip"
app:riv_oval="true"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/song_image_margin"/>
<TextView
android:singleLine="true"
android:ellipsize="end"
android:layout_width="fill_parent"
android:textSize="@dimen/song_title_text_size"
android:id="@+id/titleListTextView"
android:layout_height="wrap_content"
android:text="@string/song_title"
android:layout_marginStart="@dimen/song_title_margin"
android:layout_centerVertical="true"
android:paddingBottom="16dp"/>
<TextView
android:singleLine="true"
android:ellipsize="end"
android:layout_width="wrap_content"
android:id="@+id/artistListTextView"
android:layout_height="wrap_content"
android:text="@string/song_artist"
android:textSize="@dimen/song_artist_text_size"
android:layout_marginStart="@dimen/song_title_margin"
android:layout_centerVertical="true"
android:paddingTop="20dp"/>
</RelativeLayout>
如果有人可以帮助我,那就太棒了。
答案 0 :(得分:0)
您要将标记设置为同一视图两次;首先是Uri
,然后是整数。由于此设置Uri
,因为标记被整数覆盖,从而产生ClassCastException。如果仔细查看,则songLay
和relativeLayout
代表相同的观点。
删除以下行,它会起作用。
songLay.setTag(position);