android - my listview was empty -
i have listview using customadapter
. first, try display listvew arrayadapter , item seems good. when try change them custom adapter item gone. here arraylist
public arraylist<hashmap<string,string>> getallassignment() { arraylist<hashmap<string,string>> daftarassignment = new arraylist<hashmap<string,string>>(); cursor cursor = database.rawquery("select customer.name assignment, customer where"+ " assignment.customerid=customer.customerid , employeeid = '"+globals.salesid+"'", null); cursor.movetofirst(); if (cursor.movetofirst()) { { map1 = new hashmap<string,string>(); name=cursor.getstring(cursor.getcolumnindex(dbhelper.name)); map1.put("one",string.valueof(daftarassignment.size())); map1.put("two",name); daftarassignment.add(map1); } while (cursor.movetonext()); } cursor.close(); return daftarassignment; }
here adapter
listassignment= datasource.getallassignment(); adapter = new customadapter(listassignment.this, listassignment, r.layout.item_list, new string[] {"two"}, new int[] {r.id.edtype});
and customadapter()
package ims.app.mobileorder; import java.util.arraylist; import java.util.hashmap; import android.content.context; import android.graphics.color; import android.view.view; import android.view.viewgroup; import android.widget.simpleadapter; public class customadapter extends simpleadapter{ private int[] colors = new int[] { color.parsecolor("#3bb9ff"), color.parsecolor("#306eff") }; public customadapter(context context, arraylist<hashmap<string,string>> mylist, int itemto, string[] from, int[] to) { super(context, mylist, itemto, from, to); } @override public view getview(int position, view convertview, viewgroup parent) { view view = super.getview(position, convertview, parent); int colorpos = position % colors.length; view.setbackgroundcolor(colors[colorpos]); return view; } }
your customadapter should :
public class customadapter extends baseadapter { public static final string key_id = "id"; public static final string key_title = "title"; public static final string key_message = "message"; private activity activity; private arraylist<hashmap<string, string>> data; private static layoutinflater inflater=null; public customadapter(activity activity, arraylist<hashmap<string, string>> data) { this.activity = activity; this.data=data; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); } public int getcount() { return data.size(); } public object getitem(int position) { return position; } public long getitemid(int position) { hashmap<string, string> map = new hashmap<string, string>(); map = data.get(position); long id = long.valueof(map.get(key_id)); return id; } public view getview(int position, view convertview, viewgroup parent) { view view = convertview; if(convertview==null) { view = inflater.inflate(r.layout.custom_list_item, null); } textview title = (textview)view.findviewbyid(r.id.list_item_title); textview message = (textview)view.findviewbyid(r.id.list_item_message); hashmap<string, string> map = new hashmap<string, string>(); map = data.get(position); title.settext(map.get(key_title)); message.settext(map.get(key_message)); return view; } }
and when adding item on activity :
arraylist<hashmap<string, string>> data = new arraylist<hashmap<string, string>>(); for(int = 0; < 10; i++) { hashmap<string, string> map = new hashmap<string, string>(); map.put(customadapter.key_id, string.valueof(i)); map.put(customadapter.key_title, "title " + i); map.put(customadapter.key_message, "message " + i); data.add(map); } madapter = new customadapter(this, data); mlist.setadapter(madapter);
and xml :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="4dip" > <textview android:id="@+id/list_item_title" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textstyle="bold" /> <textview android:id="@+id/list_item_message" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textstyle="italic" /> </linearlayout>
Comments
Post a Comment