0

in my sqlite db i have one table

Name: category

column name: items (only one column) rows in items column: abc, xyz, etc...

but i dont want to create custom navigation drawer. so i added default navigation drawer activity.

in my DatabaseHandler.java there is a code to retrieve all items in arraylist as follows:

public ArrayList<menu> getAllMenu() {
        ArrayList<menu> catList = new ArrayList<menu>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + CATEGOTY_TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                menu menu = new menu();

                menu.setItem(cursor.getString(0));

        // Adding category to list
                catList.add(menu);
            } while (cursor.moveToNext());
        }
        // return category list
        return catList;
    }

MainActivity.java contains:

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

i have removed all default menu items, because i dont want to put them in xml file. i want to populate them from database.

but the question is, how can i call databasehandler.java to get that array list and put it in navigationmenu

plz help...

varsha valanju
  • 801
  • 1
  • 9
  • 27
  • Considering DatabaseHandler extends Sqliteopenhelper class. So, you can create DatabaseHandler object in activity class & call method getAllMenu() which returned list can set to navigationView. – VVB Oct 25 '16 at 04:51
  • means items=db.getAllMenu();. right? – varsha valanju Oct 25 '16 at 04:55
  • but how i can add that items arraylist to navigation menu? – varsha valanju Oct 25 '16 at 04:56
  • Yes exactly.... – VVB Oct 25 '16 at 04:56
  • i m getting all data from db to arraylist but i dont know how to add that array list to navigation menu...so that it can be visible in navigation drawer menu – varsha valanju Oct 25 '16 at 04:59
  • Some confusion. You need to have a design of XML like this where you can set list to listview inside navigationview. Refer xml http://stackoverflow.com/questions/30978114/add-a-listview-or-recyclerview-to-new-navigationview – VVB Oct 25 '16 at 05:06
  • you can do this in multiple way as suggested by @VVB , you can also fix it like this ,https://androidbelieve.com/dynamic-navigation-drawer-items/ no need to set listview inside navigationview .Just make one arrylist that you want to fill as menu item . – Radhey Oct 25 '16 at 05:09
  • thanx radhey, but sorry i already reffered that link.but i m new to android, and currently i dont have enough knowledge of json, networking etc.. so i think i ll refer link given by @VVB...i ll try it – varsha valanju Oct 25 '16 at 05:14

1 Answers1

2

Adding following code, after initialization of navigation menu, gave me desired output:

    NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);// initialization of navigation menu
    navigationView.setNavigationItemSelectedListener(this);//adding listener to navigation menu

    List<String> item = db.getAllMenu();//getting data from database

    ListView lv=(ListView)findViewById(R.id.list_view_inside_nav);//initialization of listview

    String[] lv_arr = new String[item.size()];//creating a String[] just as the size of the data retrieved from database

    //adding all data from item list to lv_arr[]
    for(int i=0;i<item.size();i++){
        lv_arr[i]= String.valueOf(item.get(i));
    }

    //setting adapter to listview
    lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
           android.R.layout.simple_list_item_1, lv_arr));  

Done!!! :D

varsha valanju
  • 801
  • 1
  • 9
  • 27