scrollview - Replaced drawable images in the custom listAdapter in android changes after scrolling the listview. Why? -
i use custombaseadapter represent list in listview. mainactivity shown in first image below in link image contains custom listview adapter. , when click 1 of items @ position number [4] drawable images changes filling color in image second. , again when scroll listview filling drawble color disappered , remains same in previous state. how can make happen??
image link understand clearly..
singlerow.java
package androidhiveexample.jsonparsing.circularimageview; import android.content.context; public class singlerow { private string title , number ; private int image; context context; public singlerow(context context){ this.context = context; } public singlerow(int image , string title , string number){ this.title = title; this.image = image; this.number = number; } public int getimage() { return image; } public void setimage(int image) { this.image = image; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getnumber() { return number; } public void setnumber(string number) { this.number = number; } }
customlistadapter.java
package androidhiveexample.jsonparsing.circularimageview; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import java.util.arraylist; public class customlistadapter extends baseadapter { arraylist<singlerow> rowarraylist; context context; public customlistadapter(context context , arraylist<singlerow> rowarraylist) { this.context = context; this.rowarraylist = rowarraylist; } @override public int getcount() { return rowarraylist.size(); } @override public object getitem(int i) { return rowarraylist.get(i); } @override public long getitemid(int i) { return rowarraylist.indexof(getitem(i)); } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); if(convertview == null){ convertview = inflater.inflate(r.layout.single_row_view , null); holder = new viewholder(); holder.title = (textview) convertview.findviewbyid(r.id.textview); holder.imageview = (imageview) convertview.findviewbyid(r.id.imageview); holder.number = (textview) convertview.findviewbyid(r.id.textviewnumber); convertview.settag(holder); }else{ holder = (viewholder) convertview.gettag(); } singlerow singlerow = (singlerow) getitem(position); holder.title.settext(singlerow.gettitle()); holder.imageview.setimageresource(singlerow.getimage()); holder.number.settext(singlerow.getnumber()); return convertview; } private class viewholder { textview title , number; imageview imageview; } }
mainactivity.java
package androidhiveexample.jsonparsing.circularimageview; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.imageview; import android.widget.listview; import java.util.arraylist; public class mainactivity extends actionbaractivity { listview lv; string number[] = {"1" , "2" , "3" ,"4" , "5" }; string title[] = {"title_1_titlename" , "title_1_titlename" , "title_1_titlename" ,"title_1_titlename" , "title_1_titlename" }; int icons[] = {r.drawable.one , r.drawable.two , r.drawable.three , r.drawable.four , r.drawable.five}; arraylist<singlerow> singlerowarray = new arraylist<>(); singlerow singlerow; static imageview iv; customlistadapter customlistadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lv = (listview) findviewbyid(r.id.listview); loopthroughrows(); lv.addheaderview(getlayoutinflater().inflate(r.layout.header, null)); customlistadapter = new customlistadapter(this , singlerowarray); lv.setadapter(customlistadapter); imageview iv1 = (imageview) findviewbyid(r.id.imageviewheader); iv1.setimageresource(r.drawable.new_img); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { position = position - 1; switch (position) { case 0: iv = (imageview) view.findviewbyid(r.id.imageview); // iv.setimageresource(r.drawable.one_background); log.i("imageview_id", iv.tostring()); fragment_1 fr = new fragment_1(); fragmentmanager fm = getfragmentmanager(); fragmenttransaction fragmenttransaction = fm.begintransaction(); fragmenttransaction.replace(r.id.fragment_container, fr); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); break; case 1: iv = (imageview) view.findviewbyid(r.id.imageview); iv.setimageresource(r.drawable.two_background); break; case 2: iv = (imageview) view.findviewbyid(r.id.imageview); iv.setimageresource(r.drawable.three_background); break; case 3: iv = (imageview) view.findviewbyid(r.id.imageview); iv.setimageresource(r.drawable.four_background); break; case 4: iv = (imageview) view.findviewbyid(r.id.imageview); iv.setimageresource(r.drawable.five_background); break; } } }); } public void loopthroughrows(){ for(int = 0 ; < title.length ; i++){ singlerow = new singlerow(icons[i] , title[i] , number[i]); singlerowarray.add(singlerow); } } @override public void onbackpressed() { fragmentmanager fm = getfragmentmanager(); if (fm.getbackstackentrycount() > 0) { fm.popbackstack(); } else { super.onbackpressed(); } } }
drawable/one.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:thickness="5dp" android:uselevel="false" > <size android:height="50dp" android:width="50dp"/> <solid android:color="#64dd17" /> </shape>
create or add field in model class staus of item in list , manipulate in getview . below ..
public class statusbean{ private boolean isactive; public string isactive() { return isactive; } public void setnameactive(boolean isactive) { this.isactive=isactive; }
}
and set these fields active state in when button click. , call
adapter.notifydatasetchanged();
and inside getview use if-else change image accordingly. let me know if face problem in it
Comments
Post a Comment