【发布时间】:2020-01-30 14:52:03
【问题描述】:
我有一个从数据库中填充的列表视图。该列表显示了可用的食物以及以下详细信息:
- 准备食物的时间
- 餐厅评分
- 餐厅的纬度/经度
- 食物的数量。
现在我想根据这四个项目对列表视图进行排序。我正在使用 ArrayList<HashMap<String, String>> 和扩展 baseadapter 的适配器。
我在 Collections.sort 中看到了很多关于 SO 的答案,但我未能在我的代码中实现它。
注意:我没有为 AvailableFood 对象定义数据模型来填充 arrayList。 这是我的代码:
AvailableFood.java
public class AvailableFood extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_available_food, container, false);
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_sort:
PopupMenu popup = new PopupMenu(getContext(), rootview.findViewById(R.id.nav_sort));
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sort_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.sort_recent: sortAvailableFood("Time");
break;
case R.id.sort_qty: sortAvailableFood("Qty");
break;
case R.id.sort_rating: sortAvailableFood("Rating");
break;
case R.id.sort_distance: sortAvailableFood("Distance");
break;
}
return true;
}
});
popup.show();
}
return false;
}
});
lvAvlFood = rootview.findViewById(R.id.list_avl_food);
arrListAvlFood = new ArrayList<HashMap<String, String >>();
arrListLoc = new ArrayList<HashMap<String, String>>();
return rootview;
}
private void sortAvailableFood(String by) {
switch (by){
case"Time":
break;
case"Qty":case"Qty":
Collections.sort(arrListAvlFood, new Comparator<HashMap<String, String>>() {
@Override
public int compare(HashMap<String, String> obj1, HashMap<String, String> obj2) {
if( Double.parseDouble(obj1.get(AvailableFood.KEY_QTY)) > Double.parseDouble(obj2.get(AvailableFood.KEY_QTY)) ) {
return 1;
}
else {
return 0;
}
}
});
AvlFoodAdapter sortedAdapter = new AvlFoodAdapter(getActivity(),arrListAvlFood);
lvAvlFood.setAdapter(sortedAdapter);
break;
case"Rating":
break;
case"Distance":
break;
}
}
}
LoadAllFood.java
class LoadAllFood extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
String ngoId = new SessionManager(getContext()).getUserId();
if(ngoId != null || !ngoId.equals("")) {
params.add(new BasicNameValuePair("n_id", ngoId));
}
JSONObject json = jParser.makeHttpRequest(url_avl_food, "GET", params);
try {
int success = json.getInt("success");
if (success == 1) {
jaAvlFood = json.getJSONArray("avl_food");
// looping through All Food
for (int i = 0; i < jaAvlFood.length(); i++) {
JSONObject c = jaAvlFood.getJSONObject(i);
String prep_time = c.getString("prep_time");
String upload_time = c.getString("upload_time");
Double qty = c.getDouble("qty");
resName = c.getString("res_name");
String lat = c.getString("lat");
String longi = c.getString("longi");
String rating = c.getString("g_rating");
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, String> hmLatLong = new HashMap<String, String>();
map.put("f_id", f_id);
map.put("prep_time",prep_time);
map.put("upload_time",upload_time);
map.put("qty",qty.toString());
map.put("name",resName);
map.put("g_rating", rating);
hmLatLong.put("lat",lat);
hmLatLong.put("longi",longi);
arrListAvlFood.add(map);
arrListLoc.add(hmLatLong);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
【问题讨论】:
-
我的建议是为 Comparator 使用自定义类并在那里应用您的逻辑。并使用您的自定义类进行排序。
-
为什么不直接使用recyclerview?
-
你能说出这段代码有什么问题吗?为什么这不起作用?我还编辑了我的代码。
-
您应该为您的
AvailableFood类使用模型表示。这将更清晰,更容易理解,您还将从该实现中受益,因为您将能够根据The time of food preparation和其他标准对列表进行排序,这将是您的AvailableFood类的数据成员。 -
@ravi 你能提示更多关于 Model 类的信息吗?或者你能解决我到目前为止所做的事情有什么问题吗?我已经通过应用程序进行了调试,collections.sort 方法没有遇到任何断点。
标签: android arraylist android-listview android-adapter android-listadapter