java - Android change imageView resource onItemClick in Listview -


i got listview 3 textviews , 1 imageview -using simpleadapter specific xml layout

layout:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/relativelayout1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical" >      <textview         android:id="@+id/tvsongname"         android:layout_width="200dp"         android:layout_height="50dp"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:background="@drawable/plank1"         android:gravity="center"         android:text="textview"         android:textsize="14sp"         android:textstyle="bold" />      <imageview           android:id="@+id/ivfav"         android:layout_width="150dp"         android:layout_height="50dp"         android:layout_alignparentright="true"         android:layout_alignparenttop="true"         android:layout_margintop="10dp"         android:src="@drawable/plank4_empty" />      <textview         android:id="@+id/tvdate"         android:layout_width="170dp"         android:layout_height="50dp"         android:layout_alignparentleft="true"         android:layout_below="@+id/tvsongname"         android:background="@drawable/plank2"         android:gravity="center"         android:text="textview"         android:textsize="14sp"         android:textstyle="bold" />      <textview android:layout_centervertical="true"         android:id="@+id/tvartist"         android:layout_width="150dp"         android:layout_height="50dp"         android:layout_alignparentright="true"         android:layout_below="@+id/ivfav"         android:background="@drawable/plank3"         android:gravity="center"         android:text="textview"         android:textsize="14sp"         android:textstyle="bold" />  </relativelayout> 

code:

// json objects of today songs , return in array     public jsonarray getjsons_array() throws clientprotocolexception, ioexception, jsonexception {         stringbuilder url = new stringbuilder(current_url);         httpget = new httpget(url.tostring());         httpresponse r = client.execute(get);         int status = r.getstatusline().getstatuscode();         if (status == 200) {             httpentity e = r.getentity();             string data = entityutils.tostring(e);             jsonarray jsonarray = new jsonarray(data);             return jsonarray;         } else {             return null;         }      }      public class read_today_songs extends asynctask<string, integer, string> {          boolean running = true;          @override         protected void onpreexecute() {             // todo auto-generated method stub             super.onpreexecute();             pbplaylist.setvisibility(view.visible);         }          @override         protected string doinbackground(string... params) {             try {                 jsonarray = getjsons_array();                 if (jsonarray.length() > 0) {                     today_song_item = new arraylist<map<string, string>>();                     (int = 0; < jsonarray.length(); i++) {                         jsonobject jsonobject = jsonarray.getjsonobject(i);                         // singername = jsonobject.getstring("singername");                         map<string, string> map = new hashmap<string, string>();                         map.put("singername", jsonobject.getstring("singername"));                         map.put("songname", jsonobject.getstring("songname"));                         map.put("date", jsonobject.getstring("date"));                         today_song_item.add(map);                     }                     return "true";                 } else                     return "false";             } catch (exception e) {                 // todo auto-generated catch block                 e.printstacktrace();                 return null;             }         }          @override         protected void onpostexecute(string result) {             // todo auto-generated method stub             super.onpostexecute(result);             // prograssbar disapper             pbplaylist.setvisibility(view.gone);             // check if got today_song_itemarraylist<map<string, string>>()             // values             if (result.contains("true")) {                 adapter = new simpleadapter(theplaylist.this, today_song_item, r.layout.playlist_item_layout, new string[] { "singername", "songname", "date" }, new int[] { r.id.tvartist, r.id.tvsongname, r.id.tvdate });                 lvsongslist.setadapter(adapter);             } else                 fb_msg = "שגיאה התרחשה!";         }     } 

on item click methods:

lvsongslist.setonitemclicklistener(new onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                 // todo auto-generated method stub                  change_selected_song_to_fav_or_not(view);                  // textview textview_songartistfromview =                 string songartistfromview = ((textview) view.findviewbyid(r.id.tvartist)).gettext().tostring();                 string songnamefromview = ((textview) view.findviewbyid(r.id.tvsongname)).gettext().tostring();                  // hold hashmap of vlaues                 hashmap<string, string> queryvaluesmap = new hashmap<string, string>();                 queryvaluesmap.put("singername", songartistfromview);                 queryvaluesmap.put("songname", songnamefromview);                  // call function adding new hashmap db                 // insert song favs -> 1=true    -1=false                 long inserted = dbtools.insertnewfav(queryvaluesmap);                 if (inserted >= 1) {                     toast.maketext(theplaylist.this, "שיר נוסף למועדפים!", toast.length_short).show();                 } else {                     toast.maketext(theplaylist.this, "שגיאה לא מוכרת בהוספת שיר! יש לנסות שוב מאוחר יותר!", toast.length_short).show();                 }                  // delete rows --------------> delete line when done //              dbtools.deleteallrows();              }         }); 

when click item change image wanted one, changing every 5th item image too!

how can fix ?

(found outer topics speaking it, i'm using simple adapter)

you use custom adapter in listview creation , write code in block...

use code in activity...

customadapter madapter = new customadapter(this, r.layout.listitem, mlistitems); mpullrefreshlistview.setadapter(madapter); 

and here replace layout....

public class customadapter extends arrayadapter<sample> {  public arraylist<sample> mlist; public context context; public layoutinflater inflater;  public customadapter(context context, int resource, arraylist<sample> mlist) {     super(context, resource);     this.mlist = mlist;     this.context = context;     inflater = (layoutinflater) context             .getsystemservice(context.layout_inflater_service); }  @override public int getposition(sample item) {     return super.getposition(item); }  @override public sample getitem(int position) {     return mlist.get(position); }  @override public int getcount() {     return mlist.size(); }  @override public long getitemid(int position) {     return super.getitemid(position); }  @override public view getview(int position, view convertview, viewgroup parent) {     convertview = inflater.inflate(r.layout.listitem, null);//replace layout....     textview text1 = (textview) convertview.findviewbyid(r.id.item1);     textview text2 = (textview) convertview.findviewbyid(r.id.item2);     text1.settext(mlist.get(position).getlistitem1());     text2.settext(mlist.get(position).getlistitem2());     text2.setonclicklistener(new onclicklistener() {         @override         public void onclick(view v) {             // put logic here , use custom adapter             // load data using particular custom adapter             // listview                             //change imageview here          }     });     return convertview; }   } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -