gpt4 book ai didi

java - 如何在 Android 中对组和子级可扩展 ListView 进行排序?

转载 作者:行者123 更新时间:2023-11-29 09:29:53 25 4
gpt4 key购买 nike

我在 mysql 中有两个表,它们通过 ID“id_catprods”和“ID_Cat”绑定(bind)“类别”和“产品”。到目前为止一切顺利,我正在使用带有自定义适配器的“ExpandableListView”在 Android 上工作,一切正常,但找不到用他们的产品对类别进行排序的解决方案。谁能帮帮我?

Json 结果:

{
"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;
}
}

最佳答案

如果可能,在 MySql 中查询时对它们进行排序。否则,这是一个建议。将类别存储在 ArrayList<String> 中.您可以使用 Collection.sort() 之一轻松对它们进行排序.然后,将您的产品存储在 Hashtable<String, ArrayList<Product>> 中其中键是类别名称,数据是存储产品详细信息的产品类。您可以使用 Comparator<Product> 对产品类别进行排序.您将需要相应地修改适配器。

关于java - 如何在 Android 中对组和子级可扩展 ListView 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23370225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com