我有一个活动,在listview中有来自sqlitedatabase的记录。 我有OptionMenu有一个“删除图标” 我想在listview中没有记录时隐藏optionMenu(listview为空)。 并在listview中有数据时显示optionMenu。 我试了很多,搜索了那个。但是找不到解决方案。
这是我的Actiivity类
package com.munawwar.sultan.cursoradapter;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.munawwar.sultan.R;
import com.munawwar.sultan.adapter.DBAdapter;
public class MeterAdapter extends Activity {
DBAdapter dbh;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_records);
dbh = new DBAdapter(this);
populateListview();
}
public void populateListview() {
SimpleCursorAdapter mycursor;
Cursor cursor;
cursor = dbh.getAllMeter();
startManagingCursor(cursor);
String[] fromfields = new String[] { DBAdapter.COLUMN_METER_DATE,
DBAdapter.COLUMN_METER_START, DBAdapter.COLUMN_METER_END };
int[] tofields = new int[] { R.id.meterdate, R.id.meterstart,
R.id.meterend };
mycursor = new SimpleCursorAdapter(this, R.layout.row_meter, cursor,
fromfields, tofields, 0);
ListView mylist = (ListView) findViewById(R.id.list);
// view if there is no record
View empty = getLayoutInflater().inflate(R.layout.no_record_found,
null, false);
addContentView(empty, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
mylist.setEmptyView(empty);
// mylist.setEmptyView(findViewById(android.R.id.empty));
mylist.setAdapter(mycursor);
}
// ===============================================================================
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_delete:
try {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage(
"Are you sure you want to delete all Traveling data?")
.setNegativeButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// *********
db = dbh.getWritableDatabase();
dbh.DeleteAllMeter();
db.close();
populateListview();
dialog.dismiss();
}
})
.setPositiveButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// *******************
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
}
答案 0 :(得分:0)
将此语句移出populateListView()
方法之外,并使其成为类的成员变量:
SimpleCursorAdapter mycursor;
onPrepareOptionsMenu()
中的执行此操作:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
int count = myCursor.getCount();
if (count == 0) {
return false; //Don't show options menu if list is empty
}
return true;
}