android:在OnPrepareOptionsMenu中需要帮助

阿西夫·汗

我有一个活动,它具有来自sqlitedatabase的列表视图中的记录。我有一个带有“删除图标”的OptionMenu我想在listview中没有记录(listview为空)时隐藏optionMenu。并在listview中有数据时显示optionMenu。我尝试了很多,寻找了。但找不到解决方案。

这是我的动感课

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);
}

}
大卫·水

将此语句移到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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

android:在OnPrepareOptionsMenu中需要帮助

来自分类Dev

在片段中需要帮助(Android)

来自分类Dev

在Android View Pager中需要帮助

来自分类Dev

在GUI中需要帮助

来自分类Dev

在VBA中需要帮助

来自分类Dev

在 $_GET 中需要帮助

来自分类Dev

需要帮助了解onCreate过程中添加Android片段

来自分类Dev

在developers.android.com教程中需要帮助

来自分类Dev

在Xamarin.android中需要帮助的时间选择器

来自分类Dev

在Powershell中的何处需要帮助

来自分类Dev

在Powershell中的何处需要帮助

来自分类Dev

CheckListBox中的字典需要帮助

来自分类Dev

在SQL连接中需要帮助

来自分类Dev

在C中需要简单的帮助

来自分类Dev

在列表 Java 中需要帮助

来自分类Dev

需要帮助,Android活动从右向左过渡

来自分类Dev

Android Studio-需要帮助创建按钮

来自分类Dev

需要帮助以获取内容视图的高度-Android

来自分类Dev

Android Studio-需要帮助创建按钮

来自分类Dev

需要帮助 android:layout_centerVertical="true"

来自分类Dev

Android 导游应用需要帮助和建议

来自分类Dev

需要帮助,在Android中具有多个按钮的“自定义视图”

来自分类Dev

需要帮助修复错误并在我的 Android 应用中添加 FCM 代码

来自分类Dev

需要帮助识别表中的公仔

来自分类Dev

需要帮助在VBA中优化SUMIFS

来自分类Dev

C#中需要的REGEX帮助

来自分类Dev

需要帮助在D中并行遍历dag

来自分类Dev

需要帮助以了解python中的代码

来自分类Dev

在SQL Server远程访问中需要帮助