im trying display drawables packages installed on android device example: "com.android.browser" show icon application. have setup sherlocklistfragment layout
xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/appname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="medium text" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/numopen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="medium text" android:textappearance="?android:attr/textappearancemedium" /> </linearlayout>
then use hashmap handle data simpleadapter
sherlocklistfragment
public class list_fragment extends sherlocklistfragment { private packagemanager pk; string[] apps = new string[] { "browser", "email", "facebook", "clock", "map", "calendar", "settings", "media player", "google play", "testapp" }; string[] period = new string[]{ "54", "23", "42", "23", "14", "23", "23", "1", "10", "12" }; public final static string table_logs = "logs"; public final static string id_col = "_id"; public final static string date_col = "date"; public final static string package_col = "package"; public final static string lat_col = "lat"; public final static string lng_col = "lng"; public final static string dur_col = "duration"; view view; private onitemclicklistener monitemclicklistener; @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { list<hashmap<string,object>> alist = new arraylist<hashmap<string,object>>(); pk = getsherlockactivity().getpackagemanager(); drawable drawable = null; for(int i=0;i<10;i++){ hashmap<string, object> hm = new hashmap<string,object>(); hm.put("app", "" + apps[i]); hm.put("per", period[i] + " seconds"); try { drawable = pk.getapplicationicon("com.android.browser"); //hm.put("image", drawable); hm.put("image", r.drawable.down_arrow); } catch (namenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } alist.add(hm); } log.d("drawable", " "+ drawable); simpleadapter listadapter = new simpleadapter(inflater.getcontext(), alist, r.layout.list_layout, new string[] {"image", "app", "per"}, new int[] {r.id.imageview, r.id.appname, r.id.numopen}); //listadapter.setviewbinder(mviewbinder); setlistadapter(listadapter); log.d("string " , " " + alist); return super.oncreateview(inflater, container, savedinstancestate); } private final simpleadapter.viewbinder mviewbinder = new simpleadapter.viewbinder() { @override public boolean setviewvalue(view view, object data, string textrepresentation) { // todo auto-generated method stub if(view instanceof imageview){ ((imageview) view).setimagedrawable((drawable) data); } return false; } }; }
edit: code
this shows text appname , arrow icon on imageview, doenst show drawable have inserted in simple adapter? see commented line:
//hm.put("image", drawable);
what doing wrong?
there may ways that, first of all, fragment layout should have listview
.
screen layout
listfragment has default layout consists of single list view. however, if desire, can customize fragment layout returning own view hierarchy oncreateview(layoutinflater, viewgroup, bundle). this, view hierarchy must contain listview object id "@android:id/list" (or list if it's in code)
optionally, view hierarchy can contain view object of type display when list view empty. "empty list" notifier must have id "android:empty". note when empty view present, list view hidden when there no data display. android documentation
as using sherlocklistfragment
layout must have listview
predefined android:id=@android:id/list
row layout
you can specify layout of individual rows in list. specifying layout resource in listadapter object hosted fragment (the listadapter binds listview data; more on later).
a listadapter constructor takes parameter specifies layout resource each row. has 2 additional parameters let specify data field associate object in row layout resource. these 2 parameters typically parallel arrays. android documentation
you must define row layout using separate xml or build dynamic each row. first option more commom.
as @johannes said simpleadapter
must have textview
. here can se example custom layout using textview
. have use/extends listadapter
, instance arrayadapter
or baseadapter
.
the important thing method getview
, method responsible return view put on each line of listview
. in method, must inflate row layout , fill it.
this link show complex custom layout example using baseadapter. can take @ link show basic custom layout is better understand have do.
Comments
Post a Comment