mercredi 1 avril 2015

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

I would like to display an alertDialog when there is no data stored in my database.However what i have tried seems not to achieve the desired goal as the alert dialog is not called when the database is empty.


Here is how i check for the existance of tables in my 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 my 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 it to the 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 to basket", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(MyBasket.this, MainActivity.class);
startActivity(i);
}
});
adb.show();
}



Any help will be appreciated.Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire