android - custom listview adapter selected item increment textview -
i used https://github.com/wdullaer/swipeactionadapter swipe each items on listview
once swipe 1 of item, textview text increment one. problem if scroll list, textview return each default value 0 , hidden items incrementing.
code onswipe event:
switch (direction) { case swipedirections.direction_far_left: selectedtext = (textview) getviewbyposition(position, getlistview()).findviewbyid(r.id.txtnumber); selectedtext.settext(string.valueof(integer.parseint(selectedtext.gettext().tostring()) + 1)); break;
and adapter code:
jsonarray jsonarray = null; try { jsonarray = new jsonarray(data); } catch (jsonexception e) { e.printstacktrace(); } string[] strarr = new string[jsonarray.length()]; arraylist<string> arraylist = new arraylist<string>(); (int = 0; < jsonarray.length(); i++) { try { strarr[i] = jsonarray.getjsonobject(i).getstring("name"); arraylist.add(jsonarray.getstring(i)); stringadapter = new arrayadapter<string>( this, r.layout.items, r.id.txtname, new arraylist<string>(arrays.aslist(strarr)) ); setlistadapter(stringadapter); stringadapter.notifydatasetchanged(); } catch (jsonexception e) { e.printstacktrace(); } }
items.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="100sp" android:background="@drawable/listview_style" android:padding="8dp" android:descendantfocusability="blocksdescendants"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageview" android:src="@mipmap/ic_launcher" android:layout_centervertical="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new text" android:id="@+id/txtname" android:textsize="20sp" android:gravity="center" android:ellipsize="none" android:singleline="false" android:scrollhorizontally="false" android:layout_centervertical="true" android:layout_marginleft="20sp" android:layout_marginright="20sp" android:layout_torightof="@+id/imageview" android:layout_toleftof="@+id/txtnumber" android:layout_tostartof="@+id/txtnumber"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:id="@+id/txtnumber" android:textsize="25sp" android:layout_centervertical="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_marginright="40dp" android:layout_marginend="40dp" /> </relativelayout>
i'm thinking of item position not valid or view not valid. anyidea how solve this. thanks
update incrementing working properly, item name not populating. see attached
the problem listview cheats. it's recycling view happens have same 10 views seeing. when scroll far enough view goes away, appears again view came view. this, got rid of old view, asked adapter make trashed view new view (which awesome memory , quick view creation).
this why items go away, because views recycled listview using adapter after scroll away. if want see this, try making swipe turn visibility of view invisible, you'll notice views on place being missing. because same view.
in short, swipe must change data used build view. changes view either wiped away, or mess other views (things visibility , .transform() not reset adapters), same view again.
public class swipeactivity extends appcompatactivity { swipeactionadapter madapter; private class yourcustomrowentry { string displaystring; int swipes; public yourcustomrowentry( string displaystring, int swipes) { this.swipes = swipes; this.displaystring = displaystring; } } private class holder { public textview textname, textnumber; public imageview imageview; public holder(textview textname, textview textnumber, imageview imageview) { this.textname = textname; this.textnumber = textnumber; this.imageview = imageview; } } arraylist<yourcustomrowentry> mdatayoueditthatbackstheadapter = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_swipe); (int = 1; <= 200; i++) { mdatayoueditthatbackstheadapter.add(new yourcustomrowentry("row " + i,0)); } baseadapter customadapter = new baseadapter() { @override public int getcount() { return mdatayoueditthatbackstheadapter.size(); } @override public object getitem(int position) { return mdatayoueditthatbackstheadapter.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view itemview; holder viewholder; if (convertview != null) { itemview = convertview; //if made view, , it's being recycled use that. viewholder = (holder)convertview.gettag(); //and fetch findbyviews things. } else { //if first time, inflate view. itemview = layoutinflater.from(parent.getcontext()).inflate(r.layout.items, parent, false); textview textname = (textview)itemview.findviewbyid(r.id.txtname); textview textnumber = (textview)itemview.findviewbyid(r.id.txtnumber); imageview imageview = (imageview)itemview.findviewbyid(r.id.imageview); viewholder = new holder(textname,textnumber,imageview); itemview.settag(viewholder); //store data in view's tag. } yourcustomrowentry ycre = mdatayoueditthatbackstheadapter.get(position); viewholder.textname.settext(ycre.displaystring); viewholder.textnumber.settext("" + ycre.swipes); // gotta tell string , not resource. //you set imageview saved set of data here too. return itemview; } }; listview listview = (listview)findviewbyid(r.id.myactivityslistview); // wrap content in swipeactionadapter madapter = new swipeactionadapter(customadapter); // pass reference of listview swipeactionadapter madapter.setlistview(listview); // set swipeactionadapter adapter listview listview.setadapter(madapter); // listen swipes madapter.setswipeactionlistener(new swipeactionadapter.swipeactionlistener() { @override public boolean hasactions(int position) { // items can swiped return true; } @override public boolean shoulddismiss(int position, int direction) { // dismiss item when swiping normal left return false; //return direction == swipedirections.direction_normal_left; } @override public void onswipe(int[] positionlist, int[] directionlist) { (int = 0; < positionlist.length; i++) { int direction = directionlist[i]; int position = positionlist[i]; switch (direction) { case swipedirections.direction_far_left: mdatayoueditthatbackstheadapter.get(position).swipes++; //add 1 swipes; madapter.notifydatasetchanged(); break; case swipedirections.direction_far_right: mdatayoueditthatbackstheadapter.get(position).swipes--; //subtract 1 swipes; madapter.notifydatasetchanged(); break; } } } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_swipe, 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); }
}
video of working: https://youtu.be/6wpf2ooku2u
save array use listview. need have can change , adapter build new views. notifydatasetchanged() , updates , rebuilds views, original data structure saved. meaning need modify data, not view. writes class , uses build views.
Comments
Post a Comment