android - How to display alertDialog in an activity when sqlite database is empty -


i display alertdialog when there no data stored in database.however have tried seems not achieve desired goal alert dialog not called when database empty.

here how check existance of tables in database:

public boolean checkfortables() {          boolean hastables = false;          sqlitedatabase db = this.getwritabledatabase();          cursor cursor = db.rawquery("select count(*)from" + contacts_table_name, null);          if (cursor != null && cursor.getcount() > 0) {              hastables = true;              cursor.close();          }          return hastables;      }

and in activity oncreate:

if (mydb.checkfortables()) {              showtable();              btn.setvisibility(view.visible);          } else {              showalert();              btn.setvisibility(view.gone);          }

where method showtable()

private void showtable() {          arraylist<string> array_list = mydb.getallcontacts();            arrayadapter arrayadapter;          arrayadapter = new arrayadapter(this, android.r.layout.simple_list_item_1, array_list);            //adding list view.          obj = (listview) findviewbyid(r.id.listview1);          obj.setadapter(arrayadapter);      }

and method showalert()

public void showalert() {          layoutinflater inflater = (layoutinflater) this.getsystemservice(layout_inflater_service);          view layout = inflater.inflate(r.layout.empty_basket, (viewgroup) findviewbyid(r.id.root));          alertdialog.builder adb = new alertdialog.builder(this);          adb.setview(layout);          adb.setcancelable(false);            adb.setpositivebutton("add items basket", new dialoginterface.onclicklistener() {              public void onclick(dialoginterface dialog, int which) {                  intent = new intent(mybasket.this, mainactivity.class);                  startactivity(i);              }          });          adb.show();      }
activity full code:

public class mybasket extends actionbaractivity {      private listview obj;      dbhelper mydb;      int numrows;      int id_to_update = 0;        @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_my_basket);          toolbar toolbar = (toolbar) findviewbyid(r.id.app_bar);          setsupportactionbar(toolbar);            getsupportactionbar().sethomebuttonenabled(true);          getsupportactionbar().setdisplayhomeasupenabled(true);          mydb = new dbhelper(this);          button btn = (button) findviewbyid(r.id.checkout);          if (mydb.checkfortables()) {              showtable();              btn.setvisibility(view.visible);          } else {              showalert();              btn.setvisibility(view.gone);          }            textview txt = (textview) findviewbyid(r.id.numrows);          mydb.numberofrows();          txt.settext(integer.tostring(numrows));              button basketbutton = (button) findviewbyid(r.id.checkout);          basketbutton.setonclicklistener(new view.onclicklistener() {              @override              public void onclick(view v) {                  alertdialog.builder alertdialog = new alertdialog.builder(mybasket.this);                  alertdialog.setcancelable(false);                  alertdialog.setmessage("done shopping?");                  alertdialog.setpositivebutton("proceed checkout", new dialoginterface.onclicklistener() {                      public void onclick(dialoginterface dialog, int which) {                          intent intent = new intent(mybasket.this, checkout.class);                          startactivity(intent);                      }                  });                    alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() {                      public void onclick(dialoginterface dialog, int which) {                          dialog.cancel();                      }                  });                  alertdialog.show();              }          });      }        public void showalert() {          layoutinflater inflater = (layoutinflater) this.getsystemservice(layout_inflater_service);          view layout = inflater.inflate(r.layout.empty_basket, (viewgroup) findviewbyid(r.id.root));          alertdialog.builder adb = new alertdialog.builder(this);          adb.setview(layout);          adb.setcancelable(false);            adb.setpositivebutton("add items basket", new dialoginterface.onclicklistener() {              public void onclick(dialoginterface dialog, int which) {                  intent = new intent(mybasket.this, mainactivity.class);                  startactivity(i);              }          });          adb.show();      }        private void showtable() {          arraylist<string> array_list = mydb.getallcontacts();            arrayadapter arrayadapter;          arrayadapter = new arrayadapter(this, android.r.layout.simple_list_item_1, array_list);            //adding list view.          obj = (listview) findviewbyid(r.id.listview1);          obj.setadapter(arrayadapter);      }          @override      public boolean oncreateoptionsmenu(menu menu) {          // inflate menu; adds items action bar if present.          getmenuinflater().inflate(r.menu.menu_my_basket, 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();            //noinspection simplifiableifstatement          if (id == r.id.action_settings) {              return true;          }          if (id == r.id.action_delete) {              mydb.deletecontact();              toast.maketext(getapplicationcontext(), "deleted successfully", toast.length_short).show();              intent intent = new intent(mybasket.this, mybasket.class);              startactivity(intent);              return true;            }          if (id == android.r.id.home) {              navutils.navigateupfromsametask(this);          }          return super.onoptionsitemselected(item);      }        public boolean onkeydown(int keycode, keyevent event) {          if (keycode == keyevent.keycode_back) {              movetasktoback(true);          }          return super.onkeydown(keycode, event);      }  }

any appreciated.thanks in advance.

the thing selecting count not rows have 1 row every time showing count zero.

public boolean checkfortables() {     boolean hasrows = false;     sqlitedatabase db = getreadabledatabase();     cursor cursor = db.rawquery("select count(*) " + table_comments, null);     cursor.movetofirst();     int count = cursor.getint(0);     if(count > 0)         hasrows = true;     db.close();     return hasrows; } 

there issue in code after solving 1 not showing dialog illegalstateexception becuase of ;)

view layout = inflater.inflate(r.layout.empty_basket, (viewgroup) findviewbyid(r.id.root)); 

you should pass null parent view because going in dialog layout .

view layout = inflater.inflate(r.layout.empty_basket, null); 

Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -