java - Use two ArrayAdapter in the same ListView -


i have these activities: coffeeactivity , drinkactivity

this main activity, has listview

public class calculationhistorylistactivity extends appcompatactivity {      save savedata;     listview listview;     arraylist arraylistdrink,arraylistcoffee;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_calculation_history_list);          // toasts test ad message on screen. remove after defining own ad unit id.        // toast.maketext(this, toast_text, toast.length_long).show();         //sqlite database          savedata = new save(calculationhistorylistactivity.this);          //listview          listview = (listview) findviewbyid(r.id.list);              //coffee list             arraylistcoffee = savedata.getallcoffeecalculation();             arrayadapter coffeearrayadapter = new arrayadapter(calculationhistorylistactivity.this,                     r.layout.list_view_item_coffee, r.id.total_money_coffee, arraylistcoffee);             listview.setadapter(coffeearrayadapter);          //drink list         arraylistdrink = savedata.getalldrinkcalculation();         arrayadapter drinkarrayadapter = new arrayadapter(calculationhistorylistactivity.this,                 r.layout.list_view_item_coffee, r.id.total_money_drink,arraylist);                 listview.setadapter(drinkarrayadapter);      }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_calculation_history_list, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }  } 

this database helper

public class save extends sqliteopenhelper {      private static final string database_name = "my.db";     private static final string table = "mytable";     private static final string coffee = "coffee";     private static final string drink = "drink";      public save(context context) {         super(context, database_name, null, 1);     }      @override     public void oncreate(sqlitedatabase sqlitedatabase) {         string query ;         query = "create table " +table+ "(" +coffee+ " text, " +drink+ " text " + ")";         sqlitedatabase.execsql(query);     }      @override     public void onupgrade(sqlitedatabase sqlitedatabase, int i, int i1) {         sqlitedatabase.execsql(" drop file if exists " + table);         oncreate(sqlitedatabase);      }      //this coffee section     public boolean adddatabasetocoffee(int i){         sqlitedatabase sqlitedatabase = getwritabledatabase();         contentvalues contentvalues = new contentvalues();         contentvalues.put(coffee,i);         long chaker = sqlitedatabase.insert(table,null,contentvalues);         if (chaker == -1){             return false;         }else {             return true;         }      }     //this drink section     public boolean adddatabasetodrink(int i){         sqlitedatabase sqlitedatabase = getwritabledatabase();         contentvalues contentvalues = new contentvalues();         contentvalues.put(drink,i);          long chaker = sqlitedatabase.insert(table,null,contentvalues);         if (chaker == -1){             return false;         }else {             return true;         }      }       //coffee     public arraylist<string> getallcoffeecalculation() {         arraylist<string> array_list = new arraylist<string>();          //hp = new hashmap();         sqlitedatabase db = this.getreadabledatabase();         cursor res =  db.rawquery("select * " + table,null);         res.movetofirst();          while(res.isafterlast() == false){             array_list.add(res.getstring(res.getcolumnindex(coffee)));             res.movetonext();         }         return array_list;     }     //drink     public arraylist<string> getalldrinkcalculation() {         arraylist<string> array_list = new arraylist<string>();          //hp = new hashmap();         sqlitedatabase db = this.getreadabledatabase();         cursor res =  db.rawquery("select * " + table,null);         res.movetofirst();          while(res.isafterlast() == false){              //array_list.add(res.getstring(res.getcolumnindex(drink)));              res.movetonext();         }         return array_list;     }  } 

my problem have 2 activities, , when use coffee or drink activity single time it's ok, if want use 2 activities both @ once, app crashes

my logcat

java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string java.lang.object.tostring()' on null object reference                                                                            @ android.widget.arrayadapter.createviewfromresource(arrayadapter.java:401)                                                                            @ android.widget.arrayadapter.getview(arrayadapter.java:369)                                                                            @ android.widget.abslistview.obtainview(abslistview.java:3065)                                                                            @ android.widget.listview.makeandaddview(listview.java:1945)                                                                            @ android.widget.listview.filldown(listview.java:719)                                                                            @ android.widget.listview.fillfromtop(listview.java:785)                                                                            @ android.widget.listview.layoutchildren(listview.java:1735)                                                                            @ android.widget.abslistview.onlayout(abslistview.java:2852)                                                                            @ android.view.view.layout(view.java:17995)                                                                            @ android.view.viewgroup.layout(viewgroup.java:5817)                                                                            @ android.widget.linearlayout.setchildframe(linearlayout.java:1742)                                                                            @ android.widget.linearlayout.layoutvertical(linearlayout.java:1585)                                                                            @ android.widget.linearlayout.onlayout(linearlayout.java:1494)                                                                            @ android.view.view.layout(view.java:17995)                                                                            @ android.view.viewgroup.layout(viewgroup.java:5817)                                                                            @ android.widget.framelayout.layoutchildren(framelayout.java:344)                                                                            @ android.widget.framelayout.onlayout(framelayout.java:281)                                                                            @ android.widget.scrollview.onlayout(scrollview.java:2537)                                                                            @ android.view.view.layout(view.java:17995)                                                                            @ android.view.viewgroup.layout(viewgroup.java:5817)                                                                            @ android.widget.relativelayout.onlayout(relativelayout.java:1080)                                                                            @ android.view.view.layout(view.java:17995)                                                                            @ android.view.viewgroup.layout(viewgroup.java:5817)                                                                            @ android.widget.framelayout.layoutchildren(framelayout.java:344)                                                                            @ android.widget.framelayout.onlayout(framelayout.java:281)                                                                            @ android.view.view.layout(view.java:17995)                                                                            @ android.view.viewgroup.layout(viewgroup.java:5817)                                                                            @ android.widget.linearlayout.setchildframe(linearlayout.java:1742)                                                                            @ android.widget.linearlayout.layoutvertical(linearlayout.java:1585)                                                                            @ android.widget.linearlayout.onlayout(linearlayout.java:1494)                                                                            @ android.view.view.layout(view.java:17995)                                                                            @ android.view.viewgroup.layout(viewgroup.java:5817)                                                                            @ android.widget.framelayout.layoutchildren(framelayout.java:344)                                                                            @ android.widget.framelayout.onlayout(framelayout.java:281)                                                                            @ android.view.view.layout(view.java:17995)                                                                            @ android.view.viewgroup.layout(viewgroup.java:5817) 

your error coming inside adapter.

java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string java.lang.object.tostring()' on null object reference   -->     @ android.widget.arrayadapter.createviewfromresource(arrayadapter.java:401)   -->     @ android.widget.arrayadapter.getview(arrayadapter.java:369)   -->     @ android.widget.abslistview.obtainview(abslistview.java:3065) 

firstly, drink list empty... commented line

//array_list.add(res.getstring(res.getcolumnindex(drink))); 

and arraylist here?

arraylistdrink = savedata.getalldrinkcalculation(); arrayadapter drinkarrayadapter = new arrayadapter(calculationhistorylistactivity.this,         r.layout.list_view_item_coffee, r.id.total_money_drink,arraylist);                                                                ^^^ 

and should drop table, not file...

sqlitedatabase.execsql(" drop file if exists " + table); 

if database worked, see nothing else wrong have. clear app data recreate database, fix these issues, should see list of drink values.


anyways, no cannot show 2 adapters in 1 listview.

you can add both columns of database 1 arraylist , display both in 1 adapter.

// coffee , drink public arraylist<string> getallcoffeecalculation() {     arraylist<string> array_list = new arraylist<string>();      sqlitedatabase db = this.getreadabledatabase();     cursor res =  db.rawquery("select * " + table,null);     res.movetofirst();      while(res.isafterlast() == false){         string c = res.getstring(res.getcolumnindex(coffee));         string d = res.getstring(res.getcolumnindex(drink));          array_list.add(c + "\t" + d);         res.movetonext();     }     return array_list; } 

then, in activity.

savedata = new save(calculationhistorylistactivity.this);  listview = (listview) findviewbyid(r.id.list);  arraylistcoffee = savedata.getallcoffeecalculation(); arrayadapter<string> coffeearrayadapter = new arrayadapter<string>(       calculationhistorylistactivity.this,       android.r.layout.simple_list_item_1,        arraylistcoffee); listview.setadapter(coffeearrayadapter); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -