android - Can't add more than one row in Listview at a Fragment -
android - Can't add more than one row in Listview at a Fragment -
i have listfragment info json. when click on list item, info sent fragment (detalhesfragment.java). if user confirms data, nail button in detalhesfragment send info list on right of main activity (pedidosfragment.java).
it's working! info middle fragment appears on first row of listview on pedidosfragment. but, if seek take item send there, row overwrited new data..isn't adding row under first row.
can help me, guys? here, codes..
detalhesfragment.java
package com.example.waitersoriginal; import android.app.fragment; import android.content.dialoginterface; import android.content.dialoginterface.onclicklistener; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.absolutelayout; import android.widget.button; import android.widget.textview; public class detalhesfragment extends fragment implements onclicklistener{ textview nomeentrada,descrentrada,valorentrada,nmarm,dsarm,vlarm; string txtnome, txtdescr; view view; @override public view oncreateview(layoutinflater inflater,viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.detalhes_fragment, container, false); button mbutton = (button) view.findviewbyid(r.id.button1); nomeentrada= (textview) view.findviewbyid(r.id.textview1); descrentrada= (textview)view.findviewbyid(r.id.textview2); valorentrada= (textview)view.findviewbyid(r.id.textview3); mbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.button1: pedidosfragment array = (pedidosfragment)getfragmentmanager().findfragmentbyid(r.id.fragment3); array.criaarray(txtnome,txtdescr); break; } } } ); homecoming view; } public void change(string txt, string txt1){ nomeentrada.settext(txt); descrentrada.settext(txt1); txtnome = txt; txtdescr = txt1; //valorentrada.settext(txt2); } @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub } }
pedidosfragment.java
package com.example.waitersoriginal; import java.util.arraylist; import java.util.arrays; import java.util.hashmap; import android.app.fragment; import android.os.bundle; import android.app.listfragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.baseadapter; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.adapterview.onitemclicklistener; public class pedidosfragment extends listfragment{ private arraylist<hashmap<string, string>> listapedidos; private static final string tag_nm = "nome"; private static final string tag_ds = "descr"; public pedidosfragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { homecoming inflater.inflate(r.layout.pedidos_fragment, container, false); } void criaarray(string nomeprod, string descrprod) { listapedidos = new arraylist<hashmap<string, string>>(); hashmap<string, string> map3 = new hashmap<string, string>(); listadapter adapter2 = new simpleadapter(getactivity(), listapedidos, r.layout.list_item, new string[] { tag_nm, tag_ds}, new int[] { r.id.name, r.id.pid}); setlistadapter(adapter2); map3.put(tag_nm, nomeprod); map3.put(tag_ds, descrprod); listapedidos.add(map3); ((baseadapter) adapter2).notifydatasetchanged(); } }
thank you..!
each time overriding adapter , creating new 1 each time, newest item.
try (assuming both fragments in memory) :
public class pedidosfragment extends listfragment{ private arraylist<hashmap<string, string>> listapedidos; private static final string tag_nm = "nome"; private static final string tag_ds = "descr"; public pedidosfragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { listapedidos = new arraylist<hashmap<string, string>>(); listadapter adapter2 = new simpleadapter(getactivity(), listapedidos, r.layout.list_item, new string[] { tag_nm, tag_ds}, new int[] { r.id.name, r.id.pid}); setlistadapter(adapter2); homecoming inflater.inflate(r.layout.pedidos_fragment, container, false); } void criaarray(string nomeprod, string descrprod) { hashmap<string, string> map3 = new hashmap<string, string>(); map3.put(tag_nm, nomeprod); map3.put(tag_ds, descrprod); listapedidos.add(map3); ((baseadapter) adapter2).notifydatasetchanged(); } }
also, avoid using constructors
fragments
, instead utilize bundles
hope helps
android json listview android-fragments
Comments
Post a Comment