gpt4 book ai didi

android - 如何使用 listview 项目 android 对 listview 进行排序?

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

我有一个从数据库中填充的 ListView 。该列表显示可用的食物以及以下详细信息:

  1. 准备食物的时间
  2. 餐厅评分
  3. 餐厅的纬度/经度
  4. 食物的数量。

现在我想根据这四项对 ListView 进行排序。我正在使用 ArrayList<HashMap<String, String>>和一个扩展基础适配器的适配器。

我已经看到很多关于 SO with Collections.sort 的答案,但我没能在我的代码中实现它。

注意:我没有为 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;
}
}

最佳答案

Food 创建一个模型类这将包含其他数据成员并使您的代码更具可读性。

 class Food {
Integer foodId;
String preparedAt;
String uploadedAt;
String name;
// other data members

//constructor


//setter for food id
public setFoodId(id){
this.foodId = id
}
//getter for food id
public getFoodId(){
return this.foodId
}


//setters and getters for other data members

//toString() method
}

接下来在您的 AsyncTask 中为 LoadAllFoods你可以这样做:

class LoadAllFood extends AsyncTask<Food, String, String>{
@Override
protected String doInBackground(Food... foods) {

}
}

现在您的 ListView 只需要一个 List<Food>你可以引用preparedAt的值或者你的适配器类上的任何其他数据成员做类似的事情:food.getFoodId();

关于android - 如何使用 listview 项目 android 对 listview 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58182650/

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