我在mysql中有两个表,它们通过id“id_catprods”和“ID_Cat”绑定“类别”和“产品”。到目前为止,一切顺利,我正在 Android 上使用“ExpandableListView”和自定义适配器,一切正常,但找不到用其产品对类别进行排序的解决方案。谁能帮我?
杰森结果:
{
"productos": [{
"id_catprods": "1",
"id_oferta": "197",
"id_producto": "1",
"nombr_cat": "Bocatas",
"id_cat": "1",
"nombre_producto": "Lomo",
"precio": "4",
"actions": []
}, {
"id_catprods": "2",
"id_oferta": "197",
"id_producto": "2",
"nombr_cat": "Pizzas",
"id_cat": "2",
"nombre_producto": "Serranito",
"precio": "4",
"actions": []
}],
"errorCode": 0,
"errorString": "Todo correcto"
}
主要代码:
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
String givemeurl;
public DownloadJSON(String url) {
this.givemeurl = id_oferta;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(Category.this);
mProgressDialog.setMessage("Cargando...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://file.php");
if(jsonobject != null){
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("productos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("nombr_cat", jsonobject.getString("nombr_cat"));
map.put("nombre_producto", jsonobject.getString("nombre_producto"));
map.put("id_cat", jsonobject.getString("id_cat"));
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
//e.printStackTrace();
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
}else{
//Log.e("Response","No data");
}
return null;
}
@Override
protected void onPostExecute(Void args) {
ExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
/*if(listview.getCount() == 0){
listview.setVisibility(View.GONE); // ERROR A CORREGIR, DOBLE CAPA?
listview.setEmptyView(findViewById(givemeempty));
TextView bottom = new TextView(Category.this);
bottom.setHeight(50); //50dp
bottom.setText("");
// Adding bottom
listview.addFooterView(bottom);
}else{
}*/
adapter = new ListViewAdapterCat(Category.this, arraylist);
ExpandableListView.setAdapter(adapter);
mProgressDialog.dismiss();
}
}
适配器代码:
public class ListViewAdapterCat extends BaseExpandableListAdapter {
Context context;
ArrayList<HashMap<String, String>> data;
//ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapterCat(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
//imageLoader = new ImageLoader(context);
}
@Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
TextView tv = null;
for (int i = 0; i < data.size(); i++) {
tv = new TextView(context);
tv.setTextSize(20);
tv.setPadding(50, 0, 0, 0);
System.out.println(data.get(i).get("id_cat"));
if(data.get(i).get("id_cat").equals("1")){
tv.setText(resultp.get(Category.nombre_producto));
}else if (data.get(i).get("id_cat").equals("2")){
tv.setText(resultp.get(Category.nombre_producto));
}else if (data.get(i).get("id_cat").equals("3")){
tv.setText(resultp.get(Category.nombre_producto));
}
}
return tv;
}
@Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return 1;
}
@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getGroupView(int arg3, boolean arg1, View convertView, ViewGroup arg4) {
ViewHolder holder;
if(convertView == null){
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.category_item, null);
holder = new ViewHolder();
holder.nombr_cat = (TextView) convertView.findViewById(R.id.nombree);
Log.e("ID DEL GRUPO", ""+arg3);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
resultp = data.get(arg3);
holder.nombr_cat.setText(resultp.get(Category.nombr_cat));
holder.nombr_cat.setTextSize(20);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
private static class ViewHolder {
public TextView nombr_cat;
public TextView nombre_producto;
}
}