你好我需要帮助通过使用游标适配器获取项目位置但是它不能正常运行并且我得到:不能被投射错误。
请帮助任何帮助将不胜感激
AlbumFragment
public class AlbumFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>,OnItemClickListener{
private Album mAlbum;
private AlbumAdapter mAdapter;
GridView gridview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_album, container, false);
mAdapter = new AlbumAdapter(getActivity(), null);
gridview = (GridView) myFragmentView.findViewById(R.id.gridView1);
getLoaderManager().initLoader(0, null, this);
return myFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gridview.setAdapter(mAdapter);
gridview.setOnItemClickListener(this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String select = null;
return new CursorLoader(getActivity(),MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
BaseColumns._ID,
/* 1 */
AlbumColumns.ALBUM,
/* 2 */
AlbumColumns.ARTIST,
/* 3 */
AlbumColumns.NUMBER_OF_SONGS,
/* 4 */
AlbumColumns.FIRST_YEAR,
/* 5 */
AlbumColumns.ALBUM_KEY
}, null, null, AlbumColumns.ALBUM);
}
@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position,
final long id) {
Cursor cursor = mAdapter.getCursor();
NavUtils.openAlbumProfile(getActivity(), cursor.getString(cursor.getColumnIndexOrThrow(AlbumColumns.ARTIST)),cursor.getString(cursor.getColumnIndexOrThrow(AlbumColumns.ALBUM)), cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID)));
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
AlbumAdapter
public class AlbumAdapter extends CursorAdapter {
private final LayoutInflater mInflater1;
public AlbumAdapter(Context context, Cursor c) {
super(context, c);
mInflater1=LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.albumTitle = (TextView) view.findViewById(R.id.albumgrid);
holder.artistName = (TextView) view.findViewById(R.id.artistgrid);
holder.coverAlbum = (ImageView)view.findViewById(R.id.icongrid);
}view.setTag(holder);
String artist = cursor.getString(cursor.getColumnIndexOrThrow(AlbumColumns.ARTIST));
String albumname = cursor.getString(cursor.getColumnIndexOrThrow(AlbumColumns.ALBUM));
long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID));
final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);
holder.coverAlbum.setBackgroundResource(R.drawable.default_artwork);
Picasso.with(context).load(albumArtUri)
.into(holder.coverAlbum);
holder.albumTitle.setText(albumname);
holder.artistName.setText(artist);}
static class ViewHolder {
TextView albumTitle;
TextView artistName;
ImageView coverAlbum;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.griditemlayout, parent, false);
}
}
相册
public class Album {
/**
* The unique Id of the album
*/
public long mAlbumId;
/**
* The name of the album
*/
public String mAlbumName;
/**
* The album artist
*/
public String mArtistName;
/**
* The number of songs in the album
*/
public int mSongNumber;
/**
* The year the album was released
*/
public String mYear;
/**
* Constructor of <code>Album</code>
*
* @param albumId The Id of the album
* @param albumName The name of the album
* @param artistName The album artist
* @param songNumber The number of songs in the album
* @param albumYear The year the album was released
*/
public Album(final long albumId, final String albumName, final String artistName,
final int songNumber, final String albumYear) {
super();
mAlbumId = albumId;
mAlbumName = albumName;
mArtistName = artistName;
mSongNumber = songNumber;
mYear = albumYear;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) mAlbumId;
result = prime * result + (mAlbumName == null ? 0 : mAlbumName.hashCode());
result = prime * result + (mArtistName == null ? 0 : mArtistName.hashCode());
result = prime * result + mSongNumber;
result = prime * result + (mYear == null ? 0 : mYear.hashCode());
return result;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Album other = (Album)obj;
if (mAlbumId != other.mAlbumId) {
return false;
}
if (!TextUtils.equals(mAlbumName, other.mAlbumName)) {
return false;
}
if (!TextUtils.equals(mArtistName, other.mArtistName)) {
return false;
}
if (mSongNumber != other.mSongNumber) {
return false;
}
if (!TextUtils.equals(mYear, other.mYear)) {
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return mAlbumName;
}
}
LogCat
12-06 09:36:23.335: E/AndroidRuntime(29125): java.lang.ClassCastException: android.content.ContentResolver$CursorWrapperInner cannot be cast to com.ray.mimp.model.Album
12-06 09:36:23.335: E/AndroidRuntime(29125): at com.ray.mimp.fragment.AlbumFragment.onItemClick(AlbumFragment.java:87)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.widget.AbsListView.performItemClick(AbsListView.java:1282)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3181)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.widget.AbsListView$3.run(AbsListView.java:3932)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.os.Handler.handleCallback(Handler.java:733)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.os.Handler.dispatchMessage(Handler.java:95)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.os.Looper.loop(Looper.java:157)
12-06 09:36:23.335: E/AndroidRuntime(29125): at android.app.ActivityThread.main(ActivityThread.java:5633)
12-06 09:36:23.335: E/AndroidRuntime(29125): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 09:36:23.335: E/AndroidRuntime(29125): at java.lang.reflect.Method.invoke(Method.java:515)
12-06 09:36:23.335: E/AndroidRuntime(29125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
12-06 09:36:23.335: E/AndroidRuntime(29125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
12-06 09:36:23.335: E/AndroidRuntime(29125): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
12-06 09:36:23.335: E/AndroidRuntime(29125): at dalvik.system.NativeStart.main(Native Method)
感谢您的帮助
答案 0 :(得分:1)
Invoking CursorAdapter.getItem(position)
returns the cursor that contains the data moved to the correct position。因为它返回Cursor,所以无法将其强制转换为相册。相反,您需要像在AlbumAdapter.bindView()