android - download a json data to a recyclerview into a fragment -
i have been working android application , using 2 tabs have created 1 fragment each , want display recyclerview fragments. problem is, when download json data server, data downloaded lost. can not see in recyclerview. have tried many times nothing happend. here code:
this fragment: public class noticia extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view view = inflater.inflate(r.layout.noticia, container, false); recyclerview = (recyclerview) view.findviewbyid(r.id.noticiasrecyclerview); recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); buscanoticia(); return view; } private void buscanoticia() { new noticiaasynctask().execute(); } class noticiaasynctask extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(getactivity()); pdialog.setmessage("aguarde..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } @override protected string doinbackground(string... params) { jsonparse jsonparse = new jsonparse(); jsonobject jsonobject = jsonparse.makehttprequest(url, "post"); log.d("dados retornados: ", jsonobject.tostring()); arraylist = new arraylist<>(); try { sucesso = jsonobject.getint("sucesso"); if (sucesso == 1) { jsonarray = jsonobject.getjsonarray("noticias"); (int = 1; <= jsonarray.length(); i++) { jsonobject dadosjson = jsonarray.getjsonobject(i); noticiaitem noticiaitem = new noticiaitem(); noticiaitem.settitulo(dadosjson.getstring("titulo")); noticiaitem.setnoticia(dadosjson.getstring("noticia")); noticiaitem.setdata(simpledateformat.parse(dadosjson.getstring("dados"))); noticiaitem.setcategoria(dadosjson.getstring("categoria")); // adiciona na lista arraylist.add(noticiaitem); } } } catch (jsonexception e) { e.printstacktrace(); } catch (parseexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(string s) { getactivity().runonuithread(new runnable() { @override public void run() { noticiaadapter = new noticiaadapter(getactivity(), arraylist); recyclerview.setadapter(noticiaadapter); } }); } } } , here adapter: public class noticiaadapter extends recyclerview.adapter<noticiaadapter.myviewholder> { // inflator recyclerview layout private context context; private list<noticiaitem> list; // construtor public noticiaadapter(context context, list<noticiaitem> list) { this.context = context; this.list = list; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.recyclerview, null); myviewholder myviewholder = new myviewholder(view); return myviewholder; } @override public void onbindviewholder(myviewholder holder, int position) { // esta eh informacao que vem banco. pego e seto no textviews dentro recyclerview noticiaitem noticia = list.get(position); // setando os dados recyclerview holder.titulo.settext(noticia.gettitulo()); holder.paragrafo.settext(noticia.getnoticia()); holder.hora.settext(string.valueof(noticia.getdata())); holder.categoria.append(noticia.getcategoria()); } @override public int getitemcount() { return (null != list ? list.size() : 0); } static class myviewholder extends recyclerview.viewholder { // recyclerview widgets protected imageview imagem; protected textview titulo; protected textview paragrafo; protected textview hora; protected textview categoria; public myviewholder(view itemview) { super(itemview); this.imagem = (imageview) itemview.findviewbyid(r.id.image_noticia); this.titulo = (textview) itemview.findviewbyid(r.id.titulo_noticia); this.paragrafo = (textview) itemview.findviewbyid(r.id.paragrafo_noticia); this.hora = (textview) itemview.findviewbyid(r.id.hora_noticia); this.categoria = (textview) itemview.findviewbyid(r.id.categoria_noticia); } } } , json data trying download is: { noticias: [ { id: "1", titulo: "titulo noticia", noticia: "noticia nova", data: "2015-07-18", categoria: "1" }, { id: "2", titulo: "titulo noticia", noticia: "noticia nova", data: "2015-07-18", categoria: "1" }, { id: "3", titulo: "titulo noticia", noticia: "noticia nova", data: "2015-07-18", categoria: "1" }, { id: "4", titulo: "importante", noticia: "mova noticia importante", data: "2015-07-20", categoria: "2" } ], sucesso: 1 } don't know whats can wrong here. please
remove runonuithread onpostexecute on same thread update ui there. more info check here android aynctask
Comments
Post a Comment