当数据库中没有数据存储时,我想显示一个alertDialog。但是我所尝试的似乎没有达到预期目标,因为当数据库为空时不会调用警告对话框。
以下是我检查数据库中表格存在的方法:
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;
}
在我的创作活动中:
if (myDb.checkForTables()) {
showTable();
btn.setVisibility(View.VISIBLE);
} else {
showAlert();
btn.setVisibility(View.GONE);
}
方法 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);
}
方法 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();
}
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 with shopping?");
alertDialog.setPositiveButton("Proceed to 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 to basket", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = 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 it to the list view.
obj = (ListView) findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my_basket, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a 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);
}
}
任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
&#39; SELECT COUNT(*)...`将返回一个游标和一个值。您需要检查返回的值以确定表是否为空,而不是查看游标结果集的大小:
public boolean checkForTables() {
boolean hasTables = false;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*)FROM" + CONTACTS_TABLE_NAME, null);
if (cursor != null) {
if( cursor.moveToFirst() ) {
if( cursor.getInt(0) > 0 ) {
hasTables = true;
}
cursor.close();
}
return hasTables;
}
答案 1 :(得分:1)
问题在于您选择的是计数而不是行,所以每次显示计数时都会有一行,即使是零。
public boolean checkForTables() {
boolean hasRows = false;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_COMMENTS, null);
cursor.moveToFirst();
int count = cursor.getInt(0);
if(count > 0)
hasRows = true;
db.close();
return hasRows;
}
在解决了没有显示对话框的问题之后,代码中还有另一个问题,你会得到IllegalStateException
因为这个问题;)
View layout = inflater.inflate(R.layout.empty_basket, (ViewGroup) findViewById(R.id.root));
您应该将null作为父视图传递,因为它将进入对话框布局
View layout = inflater.inflate(R.layout.empty_basket, null);