java - Multicolumn ListView doesn't refresh using notifyDataSetChanged() -
i'm trying make listview using multicolumn format. found examples , have changed testing. want create list containg data bluetooth connection (and need preserve position when list updated).
my activity have list , button. clicking button list should show new values replacing first letters references numbers here happens:
list loaded:
after clicking button:
here code:
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <listview android:id="@+id/listview" android:layout_height="wrap_content" android:layout_width="match_parent"> </listview> <button android:id="@+id/btnok" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="ok"> </button>
listview_row.xml
<linearlayout android:id="@+id/relativelayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <textview android:id="@+id/text1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="first" android:layout_weight="1"> </textview> <textview android:id="@+id/text2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="second" android:layout_weight="2"> </textview> <textview android:id="@+id/text3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="third" android:layout_weight="1"> </textview> <textview android:id="@+id/text4" android:layout_width="0dp" android:layout_height="wrap_content" android:text="fourth" android:layout_weight="1"> </textview>
mainactivity.java
public class mainactivity extends actionbaractivity { private arraylist<hashmap<string, string>> list; private button btnok; private listview listview; private listviewadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.listview); list = new arraylist<hashmap<string, string>>(); hashmap<string, string> temp1 = new hashmap<string, string>(); temp1.put("text1", "a1"); temp1.put("text2", "a2"); temp1.put("text3", "a3"); temp1.put("text4", "a4"); list.add(temp1); hashmap<string, string> temp2 = new hashmap<string, string>(); temp2.put("text1", "b1"); temp2.put("text2", "b2"); temp2.put("text3", "b3"); temp2.put("text4", "b4"); list.add(temp2); hashmap<string, string> temp3 = new hashmap<string, string>(); temp3.put("text1", "c1"); temp3.put("text2", "c2"); temp3.put("text3", "c3"); temp3.put("text4", "c4"); list.add(temp3); adapter = new listviewadapter(this, list); listview.setadapter(adapter); btnok = (button) findviewbyid(r.id.btnok); // create click listener view.onclicklistener oclbtnok = new view.onclicklistener() { @override public void onclick(view v) { // limpiar valor list.clear(); hashmap<string, string> temp4 = new hashmap<string, string>(); temp4.put("text1", "11"); temp4.put("text2", "12"); temp4.put("text3", "13"); temp4.put("text4", "14"); list.add(temp4); hashmap<string, string> temp5 = new hashmap<string, string>(); temp5.put("text1", "21"); temp5.put("text2", "22"); temp5.put("text3", "23"); temp5.put("text4", "24"); list.add(temp5); hashmap<string, string> temp6 = new hashmap<string, string>(); temp6.put("text1", "31"); temp6.put("text2", "32"); temp6.put("text3", "33"); temp6.put("text4", "34"); list.add(temp6); hashmap<string, string> temp7 = new hashmap<string, string>(); temp7.put("text1", "41"); temp7.put("text2", "42"); temp7.put("text3", "43"); temp7.put("text4", "44"); list.add(temp7); hashmap<string, string> temp8 = new hashmap<string, string>(); temp8.put("text1", "51"); temp8.put("text2", "52"); temp8.put("text3", "53"); temp8.put("text4", "54"); list.add(temp8); // actualizar adapter.notifydatasetchanged(); } }; // assign click listener ok button (btnok) btnok.setonclicklistener(oclbtnok); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } // adaptador para el listview private class listviewadapter extends baseadapter { public arraylist<hashmap<string, string>> list; activity activity; textview txt1; textview txt2; textview txt3; textview txt4; public listviewadapter(activity activity,arraylist<hashmap<string, string>> list){ super(); this.activity=activity; this.list=list; } @override public int getcount() { // todo auto-generated method stub return list.size(); } @override public object getitem(int position) { // todo auto-generated method stub return list.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub layoutinflater inflater=activity.getlayoutinflater(); if(convertview == null){ convertview=inflater.inflate(r.layout.listview_row, null); txt1=(textview) convertview.findviewbyid(r.id.text1); txt2=(textview) convertview.findviewbyid(r.id.text2); txt3=(textview) convertview.findviewbyid(r.id.text3); txt4=(textview) convertview.findviewbyid(r.id.text4); } hashmap<string, string> map=list.get(position); txt1.settext(map.get("text1")); txt2.settext(map.get("text2")); txt3.settext(map.get("text3")); txt4.settext(map.get("text4")); return convertview; } }
just create single temp object , reuse ..may helps ..
list = new arraylist<hashmap<string, string>>(); hashmap<string, string> temp = new hashmap<string, string>(); temp.put("text1", "a1"); temp.put("text2", "a2"); temp.put("text3", "a3"); temp.put("text4", "a4"); list.add(temp); temp.clear(); temp.put("text1", "b1"); temp.put("text2", "b2"); temp.put("text3", "b3"); temp.put("text4", "b4"); list.add(temp); temp.clear(); temp.put("text1", "c1"); temp.put("text2", "c2"); temp.put("text3", "c3"); temp.put("text4", "c4"); list.add(temp);
Comments
Post a Comment