如何对群组和孩子进行排序Android中的可扩展列表视图?

我在mysql中有两个表,通过ids’id_catprods’和’ID_Cat’绑定’Categories’和’products’。 到目前为止一直很好,我正在使用带有自定义适配器的“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 { 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>(); // 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 map = new HashMap(); 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> data; //ImageLoader imageLoader; HashMap resultp = new HashMap(); public ListViewAdapterCat(Context context, ArrayList<HashMap> 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 。 您可以使用Collection.sort()之一轻松对它们进行排序。 然后,将产品存储在Hashtable> ,其中键是类别名称,数据是存储产品详细信息的Product类。 您可以使用Comparator对Product类进行排序。 您需要相应地修改适配器。

使用

  Collections.sort(arraylist); 

要么

 list.sort(String::compareToIgnoreCase); 

这将对您的arraylist进行排序。如果您有可扩展列表,则必须使用

  Collections.sort(arraylist, new Comparator(){ @Override public int compare(Modelclass o1, Modelclass o2) { Collections.sort(o1.getsubarraylist(), new Comparator(){ @Override public int compare(Modelclass o1, Modelclass o2) { return o1.getName().compareToIgnoreCase(o2.getName()); } }); Collections.sort(o2.getsubarraylist(), new Comparator(){ @Override public int compare(Modelclass o1, Modelclass o2) { return o1.getName().compareToIgnoreCase(o2.getName()); } }); return o1.getName().compareToIgnoreCase(o2.getName()); // *This Sorts The Group's if you don't wanna sort group simply return 0*; } }); 

注意: – o1.getsubarraylist()&o2.getsubarraylist()是可扩展列表的子列表,只需添加内部Collections.sort()以对可扩展列表进行排序

或者如果你有简单字符串 arraylist

  Collections.sort(arraylist, new Comparator(){ @Override public int compare(String o1, String o2) { return o1.compareToIgnoreCase(o2); } }); 

我希望这可以帮助有需要的人……

使用产品类别进行排序的更简单方法是从mysql查询。 您可以使用inner join或任何其他适合您的案例来对产品类别进行排序和建立关系。