我有一个 ListView,我有两个来自数据库的表,它们是类别和项目,我想在 ListView 中将它们显示为标题(类别)和每个标题下方的项目。我不知道为什么我不能动态地做我用这个教程 http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
并且工作正常但数据是静态的我想从数据库中获取数据并显示它
请提供任何帮助。谢谢
这是我试图做的,但数据没有绑定
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=??
String Url="http://192.168.3.113/mywebservices.php?op=GetOrderCategory&lang_id=1&order_id="+order_id;
GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener()
{
@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
if (result!=null)
{
try
{
JSONArray jArray = new JSONArray(result);
ArrayList<Integer> category=new ArrayList<Integer>();
for (int i = 0; i < jArray.length(); i++)
{
// HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
id=e.getInt("order_id");
if (id==0)
{
}
else
{
cat_name=e.getString("cat_name");
cat_id=e.getInt("cat_id");
category.add(cat_id);
//select orderitems where cat_id??
BindCatItems(cat_id, id);
}
}
Catadapter.addSection(cat_name , new SimpleAdapter(getActivity(), catItemsList, R.layout.list_item,
new String[] {"item_name"},
new int[]{R.id.list_item_title}));
Log.i("Bind item", "CAT SIZE "+catItemsList.size());
order_items_list.setAdapter(Catadapter);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
try
{
GetNetworkData task = new GetNetworkData(Url,null,listener);
task.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected void BindCatItems(int cat_id,int order_id)
{
// TODO Auto-generated method stub
String Url="http://192.168.3.113/mywebservices.php?op=GetOrderItems&lang_id=1&cat_id='"+cat_id+"'&order_id="+order_id;
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);
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)
{
}
// String s = e.getString("posts");
// JSONObject jObject = new JSONObject(s);
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"));
catItemsList.add(map);
}
}
Log.i("Bind", "CAT SIZE "+catItemsList.size());
Log.i("Add Section","section "+cat_name);
/* ItemsAdapter=new SimpleAdapter(getActivity(), catItemsList,
R.layout.list_item,
new String[] {"item_name"},
new int[]{R.id.list_item_title});
*/
//order_list.setAdapter(adapter);
//adapter.notifyDataSetChanged();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
try
{
GetNetworkData task = new GetNetworkData(Url,null,listener);
task.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
}