Java Android应用程序能够平稳运行,但显示空白页面
在我的数据库中,我有两个表,表类别和表菜单。 在类别表中,每个餐厅都有不同的类别(即餐厅1:{类别:食品、饮料},餐厅2:{类别:零食、甜点}。现在,我想以可扩展列表的形式检索所选餐厅每个类别的菜单。类别名称作为组列表,该类别中的菜单作为子列表。到目前为止,我已成功检索数据数组,并尝试使用自己的自定义适配器创建可扩展列表,但该应用程序能够运行,但显示bl第页 MainActivity.javaJava Android应用程序能够平稳运行,但显示空白页面,java,php,android,mysql,xml,Java,Php,Android,Mysql,Xml,在我的数据库中,我有两个表,表类别和表菜单。 在类别表中,每个餐厅都有不同的类别(即餐厅1:{类别:食品、饮料},餐厅2:{类别:零食、甜点}。现在,我想以可扩展列表的形式检索所选餐厅每个类别的菜单。类别名称作为组列表,该类别中的菜单作为子列表。到目前为止,我已成功检索数据数组,并尝试使用自己的自定义适配器创建可扩展列表,但该应用程序能够运行,但显示bl第页 MainActivity.java package com.example.ed.expandablelistview; import
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://api.mydomain.com/get_menu_list.php";
private static String url_get_cat_list = "http://api.mydomain.com/get_cat_list.php";
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;
}
}
}
package com.example.ed.expandablelistview;
导入android.app.Activity;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ExpandableListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
公共类MainActivity扩展了活动{
HashMap countriesHashMap;
列出国家SHASHMAPKEYS;
ExpandableListView ExpandableListView;
MyCustomAdapter适配器;
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记\u CAT\u LIST=“CAT\u LIST”;
私有静态最终字符串TAG\u MENU\u LIST=“MENU\u LIST”;
私有静态最终字符串标记_CAT=“category”;
私有静态最终字符串标记\u ITEM=“ITEM”;
私有静态字符串url\u获取\u菜单\u列表=”http://api.mydomain.com/get_menu_list.php";
私有静态字符串url\u get\u cat\u list=”http://api.mydomain.com/get_cat_list.php";
ArrayList catList;
JSONArray菜单\列表=null;
List catFoodList=new ArrayList();
HashMap menuHashMap=新HashMap();
JSONParser jParser=新的JSONParser();
JSONArray目录列表=null;
字符串rest\u name,rest\u pid;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建ShowMenu().execute();
expandableListView=(expandableListView)findViewById(R.id.expandableList);
countriesHashMap=menuHashMap;
countriesHashMapKeys=新的ArrayList(countriesHashMap.keySet());
adapter=新的MyCustomAdapter(此、CountriesShashMap、CountriesShashMapKeys);
expandableListView.setAdapter(适配器);
}
公共类ShowMenu扩展异步任务{
受保护的字符串doInBackground(字符串…args){
rest_name=“餐厅”;
rest_pid=“2”;
catList=newarraylist();
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“rest”,rest_名称));
添加参数(新的BasicNameValuePair(“pid”,剩余pid));
//从url获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_get_cat_list,“get”,参数);
//检查cat日志以获取响应
Log.d(“菜单:,json.toString());
//获取类别列表的数组
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//获取菜单数组
cat_list=json.getJSONArray(TAG_cat_list);
//循环浏览所有类别列表
对于(int i=0;ivalue
地图放置(标签、类别);
//将哈希列表添加到arraylist
catList.add(map);
//构建参数以获取类别的菜单
List mparams=new ArrayList();
添加(新的BasicNameValuePair(“get_cat”,类别));
添加(新的BasicNameValuePair(“get_pid”,rest_pid));
//获取类别的菜单
JSONObject menujson=jParser.makeHttpRequest(url\u get\u menu\u list,“get”,mparams);
//检查cat日志以获取响应
d(“菜单:,menujson.toString());
试一试{
//检查成功标签
int msuccess=menujson.getInt(TAG_SUCCESS);
if(msuccess==1){
//获取菜单数组
menu_list=json.getJSONArray(TAG_menu_list);
对于(int m=0;m
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;
}
}
package com.example.ed.expandablelistview;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.TextView;
导入java.util.HashMap;
导入java。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>