java - Getting item position from cursor adapter -
hello need on getting item position using cursor adapter not working error occurs , : cannot cast error.
please appreciated
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); } }
album
public class album { /** * unique id of album */ public long malbumid; /** * name of album */ public string malbumname; /** * album artist */ public string martistname; /** * number of songs in album */ public int msongnumber; /** * year album released */ public string myear; /** * constructor of <code>album</code> * * @param albumid id of album * @param albumname name of album * @param artistname album artist * @param songnumber number of songs in album * @param albumyear year album 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 cast com.ray.mimp.model.album 12-06 09:36:23.335: e/androidruntime(29125): @ com.ray.mimp.fragment.albumfragment.onitemclick(albumfragment.java:87) 12-06 09:36:23.335: e/androidruntime(29125): @ android.widget.adapterview.performitemclick(adapterview.java:299) 12-06 09:36:23.335: e/androidruntime(29125): @ android.widget.abslistview.performitemclick(abslistview.java:1282) 12-06 09:36:23.335: e/androidruntime(29125): @ android.widget.abslistview$performclick.run(abslistview.java:3181) 12-06 09:36:23.335: e/androidruntime(29125): @ android.widget.abslistview$3.run(abslistview.java:3932) 12-06 09:36:23.335: e/androidruntime(29125): @ android.os.handler.handlecallback(handler.java:733) 12-06 09:36:23.335: e/androidruntime(29125): @ android.os.handler.dispatchmessage(handler.java:95) 12-06 09:36:23.335: e/androidruntime(29125): @ android.os.looper.loop(looper.java:157) 12-06 09:36:23.335: e/androidruntime(29125): @ android.app.activitythread.main(activitythread.java:5633) 12-06 09:36:23.335: e/androidruntime(29125): @ java.lang.reflect.method.invokenative(native method) 12-06 09:36:23.335: e/androidruntime(29125): @ java.lang.reflect.method.invoke(method.java:515) 12-06 09:36:23.335: e/androidruntime(29125): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:896) 12-06 09:36:23.335: e/androidruntime(29125): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:712) 12-06 09:36:23.335: e/androidruntime(29125): @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:132) 12-06 09:36:23.335: e/androidruntime(29125): @ dalvik.system.nativestart.main(native method)
thanks help
invoking cursoradapter.getitem(position)
returns cursor contains data moved correct position. because returns cursor, cannot cast album. instead, need query cursor did in albumadapter.bindview()
Comments
Post a Comment