java - android setvisibility for button in listview -
i parsing json url inside fragment , put data listview. each element have hidden button id button1 can see in xml layout.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/ler" > <!-- thumbnail image --> <com.android.volley.toolbox.networkimageview android:id="@+id/thumbnail" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignparentleft="true" android:layout_marginright="8dp"/> <!-- movie title --> <textview android:id="@+id/title" android:paddingtop="40dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/thumbnail" android:layout_torightof="@+id/thumbnail" android:textsize="@dimen/title" android:textcolor="@color/bastru" android:textstyle="bold" /> <textview android:id="@+id/desc" android:layout_below="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@+id/thumbnail" android:textsize="@dimen/desc"/> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="vezi inregistrari" android:layout_torightof="@+id/thumbnail" android:layout_below="@+id/desc" android:visibility="invisible"/> <textview android:id="@+id/link" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/thumbnail" android:layout_torightof="@+id/thumbnail" android:textsize="@dimen/title" android:textstyle="bold" android:visibility="gone"/> <textview android:id="@+id/cand" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/thumbnail" android:layout_torightof="@+id/thumbnail" android:textsize="@dimen/desc" android:textcolor="@color/verde" android:layout_marginbottom="10dp" android:layout_margintop="5dp" /> </relativelayout>
now, every json element have entry called "recordings" has value yes or no. if value yes, must set button specific row visible.
(int = 0; < response.length(); i++) { try { jsonobject obj = response.getjsonobject(i); emi movie = new emi(); if (obj.getstring("recordings").equals("yes")) { button b = (button) getview().findviewbyid(r.id.button1); b.setvisibility(view.visible); } movie.settitle(obj.getstring("titlu")); movie.setdesc(obj.getstring("descriere")); movie.setcand(obj.getstring("detalii")); movie.setthumbnailurl(obj.getstring("pozamoderator")); movie.setlink(obj.getstring("iurl")); // adding movie movies array movielist.add(movie); . . .
but app crashes error:
08-25 11:44:48.615 10248-10248/com.testapp.aacplay e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ com.testapp.aacplay.emisiuni$2.onresponse(emisiuni.java:105) @ com.testapp.aacplay.emisiuni$2.onresponse(emisiuni.java:88) @ com.android.volley.toolbox.jsonrequest.deliverresponse(jsonrequest.java:65) @ com.android.volley.executordelivery$responsedeliveryrunnable.run(executordelivery.java:99) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5227) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:562) @ dalvik.system.nativestart.main(native method)
and code adapter:
public class emisiuniadapter extends baseadapter { private activity activity; private layoutinflater inflater; private list<emi> movieitems; imageloader imageloader = appcontroller.getinstance().getimageloader(); public emisiuniadapter(activity activity, list<emi> movieitems) { this.activity = activity; this.movieitems = movieitems; } public int getcount() { return movieitems.size(); } public object getitem(int location) { return movieitems.get(location); } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { if (inflater == null) inflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); if (convertview == null) convertview = inflater.inflate(r.layout.list_emisiuni, null); if (imageloader == null) imageloader = appcontroller.getinstance().getimageloader(); networkimageview thumbnail = (networkimageview) convertview .findviewbyid(r.id.thumbnail); textview title = (textview) convertview.findviewbyid(r.id.title); textview link = (textview) convertview.findviewbyid(r.id.link); textview desc = (textview) convertview.findviewbyid(r.id.desc); textview cand = (textview) convertview.findviewbyid(r.id.cand); // getting movie data row emi m = movieitems.get(position); // thumbnail image thumbnail.setimageurl(m.getthumbnailurl(), imageloader); // title title.settext(m.gettitle()); link.settext(m.getlink()); desc.settext(m.getdesc()); cand.settext(m.getcand()); return convertview; } }
in emi
string recording; public string getrecording() { return recording; } public void setrecording(string recording) { this.recording = recording; }
in loop
if (obj.getstring("recordings").equals("yes")) { movie.setrecording("yes"); }else{ movie.setrecording("no"); }
remove this
button b = (button) getview().findviewbyid(r.id.button1); b.setvisibility(view.visible);
then in adapter getview
button b = (button) convertview.findviewbyid(r.id.button1); if(m.getrecording().equals("yes")) b.setvisibility(view.visible); else b.setvisibility(view.invisible);
also should use viewholder pattern
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Comments
Post a Comment