android - How to declare an object at class level and get intent data? -
i'm trying recive arraylist 1 activity , add data list , pass along again. thing is, when have declare @ class level question is. how data intent when declaring @ class level?
@override protected void oncreate(bundle savedinstancestate) { log.d(tag, "start oncreate listan"); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_listan); bundle listan = getintent().getextras(); arraylist<produkt> lista = (arraylist<produkt>) getintent().getserializableextra("list"); if (lista.isempty()) { log.d(tag, "lista==null"); listproduktadapter adapter = new listproduktadapter(listan.this, lista); listview listview = (listview) findviewbyid(r.id.listview); bundle produktinfo = getintent().getextras(); string name = produktinfo.getstring("name").tostring(); string allergen = produktinfo.getstring("allergen").tostring(); produkt produkt = new produkt(allergen, name); log.d(tag, "gjort produkt"); lista.add(produkt); listview.setadapter(adapter); } else { listproduktadapter adapter = new listproduktadapter(listan.this, lista); listview listview = (listview) findviewbyid(r.id.listview); bundle produktinfo = getintent().getextras(); string name = produktinfo.getstring("name").tostring(); string allergen = produktinfo.getstring("allergen").tostring(); produkt produkt = new produkt(allergen, name); log.d(tag, "gjort produkt"); lista.add(produkt); listview.setadapter(adapter); } }
here method pass along again.
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { intentresult result = intentintegrator.parseactivityresult(requestcode, resultcode, data); if(result != null) { if(result.getcontents() == null) { log.d(tag, "scanning failed"); toast.maketext(this, "cancelled", toast.length_long).show(); } else { log.d(tag, "scannad"); toast.maketext(this, "scanned: " + result.getcontents(), toast.length_long).show(); intent = new intent(this, getxml.class); i.putextra("barcode", result.getcontents()); i.putextra("list", lista); startactivity(i); log.d(tag, "going new activity"); } } else { super.onactivityresult(requestcode, resultcode, data); } }
the error "can't resolve symbol 'lista'" on i.putextra("list", lista); on "onactivityresult"
solved:
i solved problem initiating object in mainactivity , passing through flow of activities empty until started adding data it.
you can use java interfaces this. if want use approach less complexity can use following method.
instead declaring 2 separate activity files, can declare activity file inside activity class file. such as
public class outerclass{ //instance method of outer class void my_method(){ int num = 23; //method-local inner class class methodinner_demo{ public void print(){ system.out.println("this method inner class "+num); } }//end of inner class //accessing inner class methodinner_demo inner = new methodinner_demo(); inner.print(); } public static void main(string args[]){ outerclass outer = new outerclass(); outer.my_method(); } }
just can declare 2 activities in single file , can pass data 1 ease.
and declaring in manifest use following approach:
<activity android:name="com.example.foo.outeractivity$inneractivity" ></activity>
but have decalre innerclass static using
public static class inneractivity{ }
Comments
Post a Comment