xamarin - Listview adapter in a ViewPager in a fragment - How to get the Activity -
i'm trying build music browser/player of local files on devices using cursorloader. i'm using xamarin android, c#. i'm using viewpager within fragment (not fragmentactivity, since fragment part of navigation drawer).
i'm having trouble listview.adapter used show songs since i'm not able activity. when using listview adapter directly in activity works fine (i'm able see list of songs), when trying use in viewpager located using pageradapter (all within fragment). i'm not able activity in listview.adapter = new songsadapter (this,cursor,songslist). please guide me in order able pass activity adapter or let me know if i'm doing wrong in general makes not possible?
thank you. appreciated :)
julien
using system; using system.collections.generic; using system.linq; using system.text; using android.support.v4.app; using android.content; using android.os; using android.runtime; using android.util; using android.views; using android.widget; using android.support.v4.view; using android.provider; using android.database; using android.net; namespace project { public class mydevicefragment : fragment { private slidingtabscrollview mslidingtabscrollview; private viewpager mviewpager; public override view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(resource.layout.slidingtabfragment, container, false); } public override void onviewcreated(view view, bundle savedinstancestate) { var activity = activity; mslidingtabscrollview = view.findviewbyid<slidingtabscrollview>(resource.id.sliding_tabs); mviewpager = view.findviewbyid<viewpager>(resource.id.viewpager); mviewpager.adapter = new samplepageradapter(); mslidingtabscrollview.viewpager = mviewpager; } public class samplepageradapter : pageradapter { list<string> items = new list<string>(); public samplepageradapter() : base() { items.add("playlists"); items.add("songs"); items.add("albums"); items.add("artists"); items.add("genres"); } public override int count { { return items.count; } } public override bool isviewfromobject(view view, java.lang.object obj) { return view == obj; } public override java.lang.object instantiateitem(viewgroup container, int position) { view view = null; switch (position) { case 1: listview listview; view = layoutinflater.from (container.context).inflate (resource.layout.testinglistactivity, container, false); listview = view.findviewbyid<listview> (resource.id.list); list<song> songslist; var uri = mediastore.audio.media.externalcontenturi; string[] projection = { mediastore.audio.media.interfaceconsts.id, mediastore.audio.media.interfaceconsts.albumid, mediastore.audio.media.interfaceconsts.title, mediastore.audio.media.interfaceconsts.artist, }; var loader = new cursorloader (container.context, uri, projection, null, null, null); var cursor = (icursor)loader.loadinbackground (); songslist = new list<song> (); if (cursor.movetofirst ()) { { songslist.add (new song { id = cursor.getlong (cursor.getcolumnindex (projection [0])), albumid = cursor.getstring (cursor.getcolumnindex (projection [1])), title = cursor.getstring (cursor.getcolumnindex (projection [2])), artist = cursor.getstring (cursor.getcolumnindex (projection [3])) }); } while (cursor.movetonext ()); } listview.adapter = new songsadapter (this,cursor,songslist);//***********here issue********************* break; default: view = layoutinflater.from (container.context).inflate (resource.layout.pager_item, container, false); int pos = position + 1; textview txttitle = view.findviewbyid<textview> (resource.id.item_title); txttitle.text = pos.tostring (); break; } container.addview (view); return view; } public string getheadertitle (int position) { return items[position]; } public override void destroyitem(viewgroup container, int position, java.lang.object obj) { container.removeview((view)obj); } } } }
i able solve issue doing following:
- passing activity argument in: mviewpager.adapter = new mydeviceadapter(activity);
adding this.context = context constructor of samplepager adapter
public class samplepageradapter : pageradapter { activity context; list items = new list();
public samplepageradapter(activity context) : base() { this.context = context; items.add("playlists"); items.add("songs"); items.add("albums"); items.add("artists"); items.add("genres"); }
...
optional(not sur if required): did separate samplepageradapter in file able use android.app instead of android.support.v4.app;
Comments
Post a Comment