Luiz Alves
Luiz Alves

Reputation: 2645

Hide menuitem in MainActivity if Listview in Android ListFragment is empty

I have a MainActivity class with actionbar and a menu item called delete. This activity loads a ListFragment class to show messages using a single cursor adapter.

I´d like to hide the delete menuitem if there is no message in the listview or after the user deleted all messages(so,no messages in ListView).

I have implemented a interface on ListFragment class and I handle it on MainActivity. It works well but I have some doubts.

I have used cursor.getCount() in ViewBinder to check if ListView is empty.

Is there a better way to do it?

Is it a big overhead I call cursor.getCount() in setViewBinder--->ViewBinder?

Thanks in advance, Luiz

public class MainActivity extends Activity implements        MyListFragment.OnCountItemsListener{
    private boolean menu_del_visible=false;

    public void OnCountItemsChanged(boolean changed){
        menu_del_visible=changed;
        invalidateOptionsMenu();
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    menu.findItem(R.id.action_del).setVisible(menu_del_visible);

    return true;
}

////

public class MyListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

public interface OnCountItemsListener {
            public void OnCountItemsChanged(boolean changed);
        }
        private OnCountItemsListener mListener;

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mListener = (OnCountItemsListener)activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()+
                        "must implement OnCountItemsListener");
            }
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            now = new Date();

            adapter = new SimpleCursorAdapter(getActivity(),
                    R.layout.msg_list_item,
                    null,
                    new String[]{DataProvider.COL_MSG, DataProvider.COL_AT},
                    new int[]{R.id.text1, R.id.text2},
                    0);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    //send callback to MainActivity to hide delete menuitem
                    mListener.OnCountItemsChanged(cursor.getCount()==0?false:true);
                    switch (view.getId()) {
.........
..........
}

////

Upvotes: 0

Views: 130

Answers (1)

Ruocco
Ruocco

Reputation: 575

First: you create a variable for your menu:

private Menu menu;

Second: inizialize the variable:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu.main, menu);
    return true;
}

Third: you create a boolean that is true when the listView is not empty.

Fourth: wherever you change the value to that boolean variable, you call invalidateOptionsMenu() and onPrepareOptionsMenu(menu).

Fifth: you override onPrepareOptionsMenu making it hide the item you want to hide when your boolean condition is false:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem delete = menu.findItem(R.id.action_del);
    delete.setVisible(menu_del_visible);
    return true;
}

Upvotes: 0

Related Questions