Ihaveamenuwithsubmenuverysimilartotheimage.AndIalsohaveadatabasestoringcategories/subcategories.
IwouldliketogetGetinthedatabasesothatitappearsinmymenu.
CodeIamusing:
privateclassArrayListextendsAsyncTask<Void,String,String>{privatefinalStringmCat;ArrayList(Stringcat){mCat=cat;}@OverrideprotectedStringdoInBackground(Void...String){HttpURLConnectionhttpCon=null;StringBuilderresult=newStringBuilder();//Toast.makeText(LoginActivity.this,"No começo de doInbackground....",Toast.LENGTH_LONG).show();
// int id = 1;
try {
String urlLogin = "http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=tx_cat";
URL url = new URL(urlLogin);
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestProperty("Accept", "application/json");
httpCon.setRequestMethod("GET");
httpCon.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXX");
httpCon.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjE0LCJ1c2VyX2lkIjoxNCwiZW1haWwiOiJ0aGlhZ28uY2FtYXJnb0Bldm9sdXRpb25pdC5jb20uYnIiLCJmb3JldmVyIjpmYWxzZSwiaXNzIjoiaHR0cDpcL1wvMTkyLjE2OC4xLjIwN1wvYXBpXC92Mlwvc3lzdGVtXC9hZG1pblwvc2Vzc2lvbiIsImlhdCI6MTQ5NTU2MDI1MCwiZXhwIjoxNDk1NTYzODUwLCJuYmYiOjE0OTU1NjAyNTAsImp0aSXXXXXXXXX.oQP7XjL1KbM7vhYu8Jcn_GHcJtzD9lqFOlD4F7u7wrI");
httpCon.setRequestProperty("Authorization", "Basic XXXXXXXX");
InputStream in = new BufferedInputStream(httpCon.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpCon.disconnect();
}
System.out.println(result.toString());
return result.toString();
}
@Override
protected void onPostExecute(final String result) {
super.onPostExecute(result);
try {
JSONObject json = new JSONObject(result);
System.out.println(json.getString("resource"));
JSONArray array = new JSONArray(json.getString("resource"));
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObj = array.getJSONObject(i);
String cat = jsonObj.getString("tx_email");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void initDrawer() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ExpandableListView) findViewById(R.id.left_drawer);
// preparing list data
prepareListData();
drawerList.setAdapter(new NavAdapter(this, listDataHeader, listDataChild));
drawerList.setOnChildClickListener(this);
actionBarDrawerToggle =
new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.drawer_open,
R.string.drawer_close
) {
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we
//don't want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we
//don't want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataChild = new HashMap<String, List<String>>();
// Adding headers
Resources res = getResources();
String[] headers = res.getStringArray(R.array.nav_drawer_labels);
listDataHeader = Arrays.asList(headers);
Button btnaddlivro = (Button) findViewById(R.id.btnaddlivro);
btnaddlivro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity2.this, Activity_addinf.class);
startActivity(intent);
}
});
// Static method
List<String> home, friends, notifs;
String[] shome, sfriends, snotifs;
friends = Arrays.asList(cat);
sfriends = res.getStringArray(R.array.elements_Portugues);
friends = Arrays.asList(sfriends);
snotifs = res.getStringArray(R.array.elements_Matemática);
notifs = Arrays.asList(snotifs);
// Add to hashMap
listDataChild.put(listDataHeader.get(0), cat); // Header, Child data
listDataChild.put(listDataHeader.get(1), friends);
listDataChild.put(listDataHeader.get(2), notifs);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
).show();
return false;
}
}