1

需要幫助我有一個活動有從sqlitedatabase記錄在列表視圖中。 我有OptionMenu有一個「刪除圖標」 我想在listview中沒有記錄(listview爲空)時隱藏optionMenu。 並在listview中有數據時顯示optionMenu。 我試了很多,尋找的。但找不到解決方案。android:在OnPrepareOptionsMenu

這裏是我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

移動這一說法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; 
} 
+0

謝謝@david wasser!它的工作,但現在我有另一個問題。 當我從列表中刪除記錄。該列表變空。菜單仍然顯示。我怎樣才能隱藏,當我刪除所有的記錄,刷新後的選項菜單也隱藏了? –

+1

兄弟! ive通過使用invalidateOptionsmenu()完成該操作; 再次感謝 –

相關問題