In my database I have 2 tables, table category, and table menu. In the category table, each restaurant has different category(i.e Restaurant1: {category: food, beverage}, Restaurant2: {category: snacks,dessert}. Now I want to retrieve the menu for each category of the selected restaurant as an expandable list. the category name as the group list and the menu in that category as the child list. So far I have managed to retrieve the array of data and tried to use my own custom adapter to create the expandablelist, but the app is able to run but shows a blank page.
MainActivity.java
package com.example.ed.expandablelistview;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity {
HashMap<String, List<String>> countriesHashMap;
List<String> countriesHashMapKeys;
ExpandableListView expandableListView;
MyCustomAdapter adapter;
private static final String TAG_SUCCESS = "success";
private static final String TAG_CAT_LIST = "cat_list";
private static final String TAG_MENU_LIST = "menu_list";
private static final String TAG_CAT = "category";
private static final String TAG_ITEM = "item";
private static String url_get_menu_list = "http://ift.tt/1N7UEJV";
private static String url_get_cat_list = "http://ift.tt/1VZuYVP";
ArrayList<HashMap<String, String>> catList;
JSONArray menu_list = null;
List<String> catFoodList = new ArrayList<String>();
HashMap<String, List<String>> menuHashMap = new HashMap<String, List<String>>();
JSONParser jParser = new JSONParser();
JSONArray cat_list = null;
String rest_name, rest_pid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ShowMenu().execute();
expandableListView = (ExpandableListView) findViewById(R.id.expandableList);
countriesHashMap = menuHashMap;
countriesHashMapKeys = new ArrayList<String>(countriesHashMap.keySet());
adapter = new MyCustomAdapter(this, countriesHashMap, countriesHashMapKeys);
expandableListView.setAdapter(adapter);
}
public class ShowMenu extends AsyncTask<String, HashMap<String, List<String>>, String> {
protected String doInBackground(String... args) {
rest_name = "Restaurant";
rest_pid = "2";
catList = new ArrayList<HashMap<String, String>>();
//building params
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("rest", rest_name));
params.add(new BasicNameValuePair("pid", rest_pid));
//getting JSON string from url
JSONObject json = jParser.makeHttpRequest(url_get_cat_list, "GET", params);
//check log cat for response
Log.d("Menu : ", json.toString());
//Get Array of category list
try {
//check for success tag
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
//get array of menu
cat_list = json.getJSONArray(TAG_CAT_LIST);
//looping through all cat_list
for (int i = 0; i < cat_list.length(); i++) {
JSONObject c = cat_list.getJSONObject(i);
//storing each json in variable
String category = c.getString(TAG_CAT);
//creating new hashmap
HashMap<String, String> map = new HashMap<String, String>();
//adding each child node to hashmap key => value
map.put(TAG_CAT, category);
//add hashlist to arraylist
catList.add(map);
//Build params to get menu of category
List<NameValuePair> mparams = new ArrayList<NameValuePair>();
mparams.add(new BasicNameValuePair("get_cat", category));
mparams.add(new BasicNameValuePair("get_pid", rest_pid));
//get menu of category
JSONObject menujson = jParser.makeHttpRequest(url_get_menu_list, "GET", mparams);
//check log cat for response
Log.d("Menu : ", menujson.toString());
try {
//check for success tag
int msuccess = menujson.getInt(TAG_SUCCESS);
if (msuccess == 1) {
//get array of menu
menu_list = json.getJSONArray(TAG_MENU_LIST);
for (int m = 0; m < menu_list.length(); m++) {
JSONObject l = menu_list.getJSONObject(m);
String item = l.getString(TAG_ITEM);
catFoodList.add(item);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
menuHashMap.put(category, catFoodList);
}
} else {
//no category found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
MyCustomAdapter.java
package com.example.ed.expandablelistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MyCustomAdapter extends BaseExpandableListAdapter {
private Context context;
private HashMap<String, List<String>> countriesHashMap;
private List<String> countryList;
public MyCustomAdapter(Context context,
HashMap<String, List<String>> hashMap,
List<String> list) {
countriesHashMap = hashMap;
this.context = context;
this.countriesHashMap = hashMap;
this.countryList = list;
}
@Override
public int getGroupCount() {
return countriesHashMap.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return countriesHashMap.get(countryList.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return countryList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return countriesHashMap.get(countryList.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String groupTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.parent_layout, parent, false);
}
TextView parentTextView = (TextView) convertView.findViewById(R.id.textViewParent);
parentTextView.setText(groupTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition,
int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String childTitle = (String) getChild(groupPosition, childPosition);
if(convertView == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout, parent, false);
}
TextView childTextView = (TextView) convertView.findViewById(R.id.textViewChild);
childTextView.setText(childTitle);
TextView childTextView2 = (TextView) convertView.findViewById(R.id.textViewChild2);
childTextView2.setText("test");
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/expandableList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/green"
android:dividerHeight="10dp"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
></ExpandableListView>
</RelativeLayout>
parent_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/textViewParent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/red"
android:textStyle="bold"/>
</LinearLayout>
child_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/textViewChild"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/orange_dark"
android:gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
/>
</LinearLayout>
I have managed to retrieve the data of the category and the menu from my database as an array, which I put in the hashmap and use the adapter to the expandablelist, but the app shows blank page. All answers and suggestions are greatly welcomed. Thanks in advance.