java - How to modify a single list item after Asynctask is done in a BaseAdapter to -


in main activity display listview uses custom baseadapter (thoughtlistadapter).

listview = (listview) findviewbyid(r.id.list); adapter = new thoughtlistadapter(this, resultingthoughts); listview.setadapter(adapter); 

every item in listview has custom layout containing textview , 2 button.

if (convertview == null) {         convertview = layoutinflater.inflate(r.layout.list_item_thought, null); }  thoughttext = (textview) convertview.findviewbyid(r.id.thought_text_view); likebutton = (button) convertview.findviewbyid(r.id.thought_like_button); dislikebutton = (button) convertview.findviewbyid(r.id.thought_dislike_button); 

when button clicked asynctask (asyncpost) called connects database , makes changes.

likebutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             system.out.println("like clicked");             thought t = thoughtitems.get(position);             thoughtid = t.getid();             opinion = 1;              asyncpost asyncpost = new asyncpost(activity,thoughtlistadapter.this);             asyncpost.execute(share_thought_url,                     tag_person_email, "m@b.it",                     tag_thought_id, thoughtid.tostring(),                     tag_opinion, opinion.tostring());         }     }); 

what need making both button-s of list item disappear after asynctask done successful outcome. have method oncomplete(jsonobject json) elaborates jsonobject returned asynctask. try make buttons non visible inside oncomplete method, doesn't work because oncomplete() doesn't know exact button has been clicked. how can pass instance of exact clicked button inside oncomplete() , make disappear , dislike buttons of concerned list item?

asyncpost global asynctask used other activities. prefer leave alone. oncomplete() method functions onpostexecute() method of asynctask. here getview() , oncomplete() methods inside baseadapter, contain code shown above. thank you.

public view getview(final int position, view convertview, viewgroup parent) {     if (layoutinflater == null) {         layoutinflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service);     }      if (convertview == null) {         convertview = layoutinflater.inflate(r.layout.list_item_thought, null);     }       thoughttext = (textview) convertview.findviewbyid(r.id.thought_text_view);     likebutton = (button) convertview.findviewbyid(r.id.thought_like_button);     dislikebutton = (button) convertview.findviewbyid(r.id.thought_dislike_button);      //thoughtitems list of custom ojbects (thought)     thought t = thoughtitems.get(position);      //here set content of current textview     thoughttext.settext(t.gettext());      //the 2 buttons same thing when clicked     likebutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             thought t = thoughtitems.get(position);             thoughtid = t.getid();             opinion = 1;              asyncpost asyncpost = new asyncpost(activity,thoughtlistadapter.this);             asyncpost.execute(share_thought_url,                     tag_person_email, "m@b.it",                     tag_thought_id, thoughtid.tostring(),                     tag_opinion, opinion.tostring());         }     });      dislikebutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             thought t = thoughtitems.get(position);             thoughtid = t.getid();             opinion = 0;              asyncpost asyncpost = new asyncpost(activity,thoughtlistadapter.this);             asyncpost.execute(share_thought_url,                                 tag_person_email, "m@b.it",                                 tag_thought_id, thoughtid.tostring(),                                 tag_opinion, opinion.tostring());         }     });      return convertview; }  @override public void oncomplete(jsonobject json) {     if (json != null) {         try {             if (json.getint(tag_success) == 0) {                 toast.maketext(activity, "operazione non riuscita.", toast.length_long).show();             } else {                 //if try make buttons of particular list item disappear                 likebutton.setvisibility(view.gone);                 dislikebutton.setvisibility(view.gone);             }         }         catch (jsonexception e) {             log.e(tag_log, "jsonexception", e);         }     }     else toast.maketext(activity, "errore connessione!", toast.length_long).show(); } 

one solution have on thought object indicate whether or not show buttons.

so in getview() method check this

likebutton = (button) convertview.findviewbyid(r.id.thought_like_button); dislikebutton = (button) convertview.findviewbyid(r.id.thought_dislike_button);  thought t = thoughtitems.get(position); if (t.hidebuttons()) {     likebutton.setvisibility(view.gone);     dislikebutton.setvisibility(view.gone);  } else {     likebutton.setvisibility(view.visible);     dislikebutton.setvisibility(view.visible);  } 

then need have oncomplete method return id of thought object related to. inside oncomplete do

int id = //get id json response for(thought t : thoughtitems) {     if (t.getid() == id) {         t.sethidebuttons(true);         notifydatasetchanged();         break;     } } 

by calling notifydatasetchanged() redraw list , when check whether should show buttons or not not show them because set on thought item


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -