Android Navigation Drawer: how to update counter value

I am starting Android development and developing a simple notepad application where I have a navigation box. Everything is in order, I can see the navigation box, and it works fine. now i'm stuck on how to update the counter value in a box. Nav Drawer displays categories of notes and their number.

How to update counter values ​​in the navigation box when a note is deleted or added (notes are saved in db with the category value - so I can get the score by doing a simple request). I carefully searched the Internet, but could not find a single example for this.

Any help would be appreciated.

+3
source share
2 answers

Navigation Drawer ( , , ). , - , , notifyDataSetChanged().

EDIT :

public class CustomAdapter extends BaseAdapter {

    private ArrayList<Object> data;

    @Override
    public int getCount() {
        //do stuff
    }

    @Override
    public Object getItem(int position) {
        //do stuff
    }

    @Override
    public long getItemId(int position) {
        //do stuff
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //do stuff
    }

    public void updateData(Cursor cursor){
        data = //fetch data from Cursor
        notifyDataSetChanged();
    }
}
+2

loadData() . ( /) , , notifyDataSetChanged(), .

public class NavDrawerListAdapter extends BaseAdapter {

    public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems)
    {
        this.context = context;
        this.navDrawerItems = navDrawerItems;
    }

    @Override
    public int getCount() 
    {
        return navDrawerItems.size();
    }

    @Override
    public Object getItem(int position) {      
        return navDrawerItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Write your code

        return convertView;
    }

    public void loadData()
    {
        DatabaseHelper db = new DatabaseHelper(context);

        // Notes and trash counts
        int countNotes = db.getAllActiveNotes().size();
        int countTrash = db.getAllTrashNotes().size();

        String strCountNotes = String.valueOf(countNotes);
        String strCountTrash = String.valueOf(countTrash);

        // Set counter value to Navigation drawer items
        navDrawerItems.get(0).setCount(strCountNotes);
        navDrawerItems.get(1).setCount(strCountTrash);

        notifyDataSetChanged();
    }
}

, loadData() onDrawerOpened(), , , FragmentActivity. , , .

public void onDrawerOpened(View drawerView) {

    // Update notes and trash counter
    adapter.loadData();

    getActionBar().setTitle(mDrawerTitle);

    invalidateOptionsMenu();            
}

, ~

+3

All Articles