我有一个很大的问题,我一直在网上搜索有一个很好的解决方案,但我找不到任何方法来解决这个问题所以我需要帮助。
我需要使用可扩展列表视图从服务器(JSON数组)获取数据,我有类别GroupData
及其项目ChildData
。我不知道如何获取每个父组的子数据...我需要类似SimpleCursorTreeAdapter
的东西,但我没有找到任何东西。
这是我到目前为止所得到的,但它不起作用所以请帮助我:
static ArrayList<HashMap<String, String>> mapList =
new ArrayList<HashMap<String, String>>();
static ArrayList<HashMap<String, String>> catItemsList =
new ArrayList<HashMap<String, String>>();
static ArrayList<ArrayList<HashMap<String, String>>> ItemsList =
new ArrayList<ArrayList<HashMap<String, String>>>();
static ListView order_list;
static ExpandableListView order_items_list;
static SimpleAdapter adapter,ItemsAdapter;
protected void BindOrederItemList(final int order_id)
{
// TODO Auto-generated method stub
//select all categories from order items where order_id =??
//select items where category=??
JSONObject params = new JSONObject();
//int no_pepole=Integer.valueOf(noOfGest_txt.getText().toString());
try
{
// params.put("order_status",myStatus);
int rest_id=prefs.getInt("Rest_id", 0);
params.put("order_id", order_id);
params.put("lang_id", 1);
params.put("rest_id", rest_id );
//params.put("order_status", 0);
// params.put("noOfpepole",number_of_guest);
}
catch(Exception e)
{
e.printStackTrace();
}
String Url="http://192.168.3.113/mywebservices.php?op=GetOrderCategory";
GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener()
{
@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
if (result!=null)
{
try
{
catItemsList.clear();
JSONArray jArray = new JSONArray(result);
ArrayList<ArrayList<HashMap<String, String>>> list=new ArrayList<ArrayList<HashMap<String, String>>>();
// ArrayList<Integer> category=new ArrayList<Integer>();
for (int i = 0; i < jArray.length(); i++)
{
HashMap<String, String> Catmap = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
id=e.getInt("order_id");
cat_name=e.getString("cat_name");
cat_id=e.getInt("cat_id");
Catmap.put("cat_id",String.valueOf(cat_id));
Catmap.put("cat_name", cat_name);
catItemsList.add(Catmap);
Log.i("Insid For Loop", "order ID "+order_id);
list= BindCatItems(cat_id, order_id);
Log.i("Insid For Loop", "Child size = "+list.size());
}
// Log.i("Insid For Loop", "Group size = "+catItemsList.size());
SimpleExpandableListAdapter expandListAdapter= new SimpleExpandableListAdapter(getActivity(),
catItemsList, R.layout.group_item,
new String[] {"cat_name"},new int[]{R.id.lbl_cat_group},
list, R.layout.category_row, new String[]{"item_name"}, new int[]{R.id.txt_category_row});
order_items_list.setAdapter(expandListAdapter);
// Log.i("Bind item", "CAT SIZE "+catItemsList.size());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
try
{
GetNetworkData task = new GetNetworkData(Url,params,listener);
task.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected ArrayList<ArrayList<HashMap<String, String>>> BindCatItems(int cat_id,int order_id)
{
// TODO Auto-generated method stub
JSONObject params = new JSONObject();
//int no_pepole=Integer.valueOf(noOfGest_txt.getText().toString());
try
{
// params.put("order_status",myStatus);
int rest_id=prefs.getInt("Rest_id", 0);
params.put("order_id", order_id);
params.put("lang_id", 1);
params.put("cat_id",cat_id );
//params.put("order_status", 0);
// params.put("noOfpepole",number_of_guest);
}
catch(Exception e)
{
e.printStackTrace();
}
String Url="http://192.168.3.113/mywebservices.php?op=GetOrderItems";
GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener()
{
@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
if (result!=null)
{
try
{
Log.i("log bind","Inside Bind Category items");
// catItemsList.clear();
mapList.clear();
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
int id=e.getInt("item_id");
if (id==0)
{
}
else
{
map.put("item_id",String.valueOf(e.getInt("item_id")));
map.put("oi_id", String.valueOf(e.getInt("oi_id")));
map.put("item_name", e.getString("item_name"));
map.put("quantity",String.valueOf( e.getString("quantity")));
map.put("price", String.valueOf("price"));
mapList.add(map);
}
}
ItemsList.add(mapList);
// Log.i("Bind Item Order", "CAT SIZE "+catItemsList.size());
/* ItemsAdapter=new SimpleAdapter(getActivity(), catItemsList,
R.layout.list_item,
new String[] {"item_name"},
new int[]{R.id.list_item_title});
*/
// Log.i("Add Section","ItemsAdapter count= "+ItemsAdapter.getCount());
//order_list.setAdapter(adapter);
//adapter.notifyDataSetChanged();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
try
{
GetNetworkData task = new GetNetworkData(Url,params,listener);
task.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
return ItemsList;
}
答案 0 :(得分:2)
根据文档,SimpleExpandableListAdapter用于静态数据。这里发生的事情可能是您的方法在获取数据之前返回,并且在已经创建适配器之后调用传递给AsyncTask的侦听器。
所以,我建议尝试的第一件事是首先获取数据,一旦执行了两个侦听器(通过检查自己定义的布尔标志或列表的长度),定义适配器并将其传递给ListView
答案 1 :(得分:0)
我把你的解决方案用于使用lisview处理两个json;
public class ListViewer extends ListActivity{
//Primary URL JSON1
private static String url0 = "http:/a.com/url";
//URL JSON2
private static String url2 = "http:/a.com/url";
//JSON1 Node names
public static final String TAG_COURSES1 = "Courses"; //JSONArray from json1
public static final String TAG_COURSEID1 = "CourseId"; //Object from json1 - Array courses
public static final String TAG_RATING1 = "Rating"; //Object from json1 - Array courses
public static final String TAG_STATUS1 = "Status"; //Object from json1 - Array courses
// JSON2 Node names
public static final String TAG_COURSES2 = "Courses"; //JSONArray from json2
public static final String TAG_COURSEID2 = "CourseId"; //Object from json2 - Array courses
public static final String TAG_TITLE = "title"; //Object from json2 - Array courses
public static final String TAG_INSTRUCTOR = "instructor"; //Object from json2 - Array courses
public static final String TAG_LENGTH = "length"; //Object from json2 - Array courses
public static final String TAG_RATING2 = "Rating"; //Object from json2 - Array courses
public static final String TAG_SUBJECT = "subject"; //Object from json2 - Array courses
public static final String TAG_DESCRIPTION = "description"; //Object from json2 - Array courses
public static final String txt_instructor = "Instructor: ";
public static final String txt_length = "Length: ";
public static final String txt_minutes = "minutes";
//Course JSONArray's from json1 & json2 = null, for now.
JSONArray Courses1 = null;
JSONArray Courses2 = null;
//Lists
public ListView list1;
public ListView list2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay_main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> coursesList1 = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> coursesList2 = new ArrayList<HashMap<String, String>>();
//Getting bundles 3 & 4, from MariposaTrainingActivity.class...
Bundle bundle1 = getIntent().getExtras();
String userName=bundle1.getString("UN"); //Getting the userName
Bundle bundle2 = getIntent().getExtras();
String password=bundle2.getString("PW"); //Getting the passord
String UR = "UserName="; //String for build the url1, for the request json1.
String QTE = "&"; //String for build the url1, for the request json1.
String PW = "Password="; //String for build the url1, for the request json1.
//Building the URL to get the json1.
String url1 = url0 + UR + userName + QTE + PW + password;
// Creating JSON Parser instance (json1)
JSONParser2 jParser1 = new JSONParser2();
// getting JSON string from URL (json1)
final JSONObject json1 = jParser1.getJSONFromUrl(url1);
// Creating JSON Parser instance (json2)
JSONParser2 jParser2 = new JSONParser2();
// getting JSON string from URL (json2)
final JSONObject json2 = jParser2.getJSONFromUrl(url2);
try {
// Getting The Array "Courses" from json1 & json2
Courses1 =json1.getJSONArray(TAG_COURSES1);
Courses2 = json2.getJSONArray(TAG_COURSES2);
int x = 1;
//LOOP FOR JSON1
for(int i = 0; i < Courses1.length(); i++){
//LOOP FOR JSON2
for(int ii = 0; ii < Courses2.length(); ii++){
JSONObject courses1 = Courses1.getJSONObject(i);
JSONObject courses2 = Courses2.getJSONObject(ii);
// Storing each json1 item in variable
int courseID1 = courses1.getInt(TAG_COURSEID1);
//Log.e("COURSEID1:", Integer.toString(courseID1));
String Rating1 = courses1.getString(TAG_RATING1);
int Status1 = courses1.getInt(TAG_STATUS1);
//Log.e("Status1:", Integer.toString(Status1)); //Put the actual value for Status1 in log.
// Storing each json2 item in variable
int courseID2 = courses2.getInt(TAG_COURSEID2);
//Log.e("COURSEID2:", Integer.toString(courseID2)); //Put the actual value for CourseID2 in log
String Title2 = courses2.getString(TAG_TITLE);
String instructor2 = courses2.getString(TAG_INSTRUCTOR);
String length2 = courses2.getString(TAG_LENGTH);
String rating2 = courses2.getString(TAG_RATING2);
String subject2 = courses2.getString(TAG_SUBJECT);
String description2 = courses2.getString(TAG_DESCRIPTION);
//Status1 = 5 from json1; Incomplete, Status1 =-1 Complete
if(courseID2 == courseID1){
int x1 = x++; //Get the count for All values.
//Log.e("x=", Integer.toString(x1)); //Show the value in Logcat.
// Get pairs/odds.
int Num = x1 % 2;
//Log.e("Num=", Integer.toString(Num)); //
//Creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
if(Num==1){
map.put(TAG_COURSEID2, Integer.toString(courseID2));
map.put(TAG_TITLE, Title2);
map.put(txt_instructor, "Instructor: ");
map.put(TAG_INSTRUCTOR, instructor2);
map.put(txt_length, "Length: ");
map.put(TAG_LENGTH, length2);
map.put(txt_minutes, " minutes");
map.put(TAG_RATING2, rating2);
map.put(TAG_SUBJECT, subject2);
map.put(TAG_DESCRIPTION, description2);
//adding HashList to ArrayList
coursesList1.add(map);
}
if(Num==0)
{
map2.put(TAG_COURSEID2, Integer.toString(courseID2));
map2.put(TAG_TITLE, Title2);
map2.put(txt_instructor, "Instructor: ");
map2.put(TAG_INSTRUCTOR, instructor2);
map2.put(txt_length, "Length: ");
map2.put(TAG_LENGTH, length2);
map2.put(txt_minutes, " minutes");
map2.put(TAG_RATING2, rating2);
map2.put(TAG_SUBJECT, subject2);
map2.put(TAG_DESCRIPTION, description2);
//adding HashList to ArrayList
coursesList2.add(map2);
}
}//if
}//for2 (json2)
} //for1 (json1)
}//Try
catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter2 = new SimpleAdapter(this, coursesList2,
R.layout.list_courses_2,
new String[] { TAG_COURSEID2, TAG_TITLE, txt_instructor, TAG_INSTRUCTOR, txt_length, TAG_LENGTH, txt_minutes, TAG_RATING2, TAG_SUBJECT, TAG_DESCRIPTION }, new int[] {
R.id.txt_courseid_r2, R.id.txt_title_r2, R.id.txt_text_instructor_r2, R.id.txt_instructor_r2, R.id.txt_text_length_r2,R.id.txt_length_r2, R.id.txt_text_minutes_r2,R.id.txt_rating_r2, R.id.txt_topic_r2, R.id.txt_description_r2 });
setListAdapter(adapter2);
colectorXML2();
list2.setAdapter(adapter2);
list2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String courseID = ((TextView) view.findViewById(R.id.txt_courseid_r2)).getText().toString();
String Title = ((TextView) view.findViewById(R.id.txt_title_r2)).getText().toString();
String instructor = ((TextView) view.findViewById(R.id.txt_instructor_r2)).getText().toString();
String length = ((TextView) view.findViewById(R.id.txt_length_r2)).getText().toString();
String rating = ((TextView) view.findViewById(R.id.txt_rating_r2)).getText().toString();
String subject = ((TextView) view.findViewById(R.id.txt_topic_r2)).getText().toString();
String description = ((TextView) view.findViewById(R.id.txt_description_r2)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleListItem.class);
in.putExtra(TAG_COURSEID2, courseID);
in.putExtra(TAG_TITLE, Title);
in.putExtra(TAG_INSTRUCTOR, instructor);
in.putExtra(TAG_LENGTH, length);
in.putExtra(TAG_RATING2, rating);
in.putExtra(TAG_SUBJECT, subject);
in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});//lv.SetOnclickListener
ListAdapter adapter = new SimpleAdapter(this, coursesList1,
R.layout.list_courses,
new String[] { TAG_COURSEID2, TAG_TITLE, txt_instructor, TAG_INSTRUCTOR, txt_length, TAG_LENGTH, txt_minutes, TAG_RATING2, TAG_SUBJECT, TAG_DESCRIPTION }, new int[] {
R.id.txt_courseid, R.id.txt_title, R.id.txt_text_instructor, R.id.txt_instructor, R.id.txt_text_length,R.id.txt_length, R.id.txt_text_minutes,R.id.txt_rating, R.id.txt_topic, R.id.txt_description });
colectorXML();
list1.setAdapter(adapter);
// Launching new screen on Selecting Single ListItem
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String courseID = ((TextView) view.findViewById(R.id.txt_courseid)).getText().toString();
String Title = ((TextView) view.findViewById(R.id.txt_title)).getText().toString();
String instructor = ((TextView) view.findViewById(R.id.txt_instructor)).getText().toString();
String length = ((TextView) view.findViewById(R.id.txt_length)).getText().toString();
String rating = ((TextView) view.findViewById(R.id.txt_rating)).getText().toString();
String subject = ((TextView) view.findViewById(R.id.txt_topic)).getText().toString();
String description = ((TextView) view.findViewById(R.id.txt_description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleListItem.class);
in.putExtra(TAG_COURSEID2, courseID);
in.putExtra(TAG_TITLE, Title);
in.putExtra(TAG_INSTRUCTOR, instructor);
in.putExtra(TAG_LENGTH, length);
in.putExtra(TAG_RATING2, rating);
in.putExtra(TAG_SUBJECT, subject);
in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});//lv.SetOnclickListener
}//onCreate
private void colectorXML() {
list1 = (ListView)findViewById(android.R.id.list);
}
private void colectorXML2() {
list2 =(ListView)findViewById(R.id.list2);
}
}// Activity
只要把你的方法和你的json信息,在这种情况下,你只需要处理1个json,所以要做其余的...希望这会给你一些想法。
答案 2 :(得分:0)
我在所有人的帮助下解决了这个问题,这就是答案
protected void BindOrederItemList(final int order_id)
{
// TODO Auto-generated method stub
//select all categories from order items where order_id =??
//select items where category=??
JSONObject params = new JSONObject();
//int no_pepole=Integer.valueOf(noOfGest_txt.getText().toString());
try
{
// params.put("order_status",myStatus);
int rest_id=prefs.getInt("Rest_id", 0);
params.put("order_id", order_id);
params.put("lang_id", 1);
params.put("rest_id", rest_id );
//params.put("order_status", 0);
// params.put("noOfpepole",number_of_guest);
}
catch(Exception e)
{
e.printStackTrace();
}
String Url="http://192.168.3.113/mywebservices.php?op=GetOrderCategory";
GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener()
{
@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
if (result!=null)
{
try
{
catItemsList.clear();
JSONArray jArray = new JSONArray(result);
ArrayList<ArrayList<HashMap<String, String>>> list=new ArrayList<ArrayList<HashMap<String, String>>>();
// ArrayList<Integer> category=new ArrayList<Integer>();
for (int i = 0; i < jArray.length(); i++)
{
HashMap<String, String> Catmap = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
id=e.getInt("order_id");
cat_name=e.getString("cat_name");
cat_id=e.getInt("cat_id");
Catmap.put("cat_id",String.valueOf(cat_id));
Catmap.put("cat_name", cat_name);
catItemsList.add(Catmap);
Log.i("Insid For Loop", "order ID "+order_id);
list= BindCatItems(cat_id, order_id);
Log.i("Insid For Loop", "Child size = "+list.size());
}
// Log.i("Insid For Loop", "Group size = "+catItemsList.size());
SimpleExpandableListAdapter expandListAdapter= new SimpleExpandableListAdapter(getActivity(),
catItemsList, R.layout.group_item,
new String[] {"cat_name"},new int[]{R.id.lbl_cat_group},
BindCatItems(cat_id, order_id), R.layout.category_row, new String[]{"item_name"}, new int[]{R.id.txt_category_row});
order_items_list.setAdapter(expandListAdapter);
// Log.i("Bind item", "CAT SIZE "+catItemsList.size());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
try
{
GetNetworkData task = new GetNetworkData(Url,params,listener);
task.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected ArrayList<ArrayList<HashMap<String, String>>> BindCatItems(int cat_id,int order_id)
{
// TODO Auto-generated method stub
ItemsList.clear();
JSONObject params = new JSONObject();
//int no_pepole=Integer.valueOf(noOfGest_txt.getText().toString());
try
{
// params.put("order_status",myStatus);
int rest_id=prefs.getInt("Rest_id", 0);
params.put("order_id", order_id);
params.put("lang_id", 1);
params.put("cat_id",cat_id );
//params.put("order_status", 0);
// params.put("noOfpepole",number_of_guest);
}
catch(Exception e)
{
e.printStackTrace();
}
String Url="http://192.168.3.113/mywebservices.php?op=GetOrderItems";
GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener()
{
@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
if (result!=null)
{
try
{
Log.i("log bind","Inside Bind Category items");
// catItemsList.clear();
ArrayList<HashMap<String, String>> mapList =
new ArrayList<HashMap<String, String>>();
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
int id=e.getInt("item_id");
if (id==0)
{
}
else
{
map.put("item_id",String.valueOf(e.getInt("item_id")));
map.put("oi_id", String.valueOf(e.getInt("oi_id")));
map.put("item_name", e.getString("item_name"));
map.put("quantity",String.valueOf( e.getString("quantity")));
map.put("price", String.valueOf("price"));
mapList.add(map);
}
}
ItemsList.add(mapList);
// Log.i("Bind Item Order", "CAT SIZE "+catItemsList.size());
/* ItemsAdapter=new SimpleAdapter(getActivity(), catItemsList,
R.layout.list_item,
new String[] {"item_name"},
new int[]{R.id.list_item_title});
*/
// Log.i("Add Section","ItemsAdapter count= "+ItemsAdapter.getCount());
//order_list.setAdapter(adapter);
//adapter.notifyDataSetChanged();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
try
{
GetNetworkData task = new GetNetworkData(Url,params,listener);
task.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
return ItemsList;
}