这是我从服务器获取数据并显示在listview
我希望按字母顺序显示列表,我在模型类的帮助下设置名称。 pojoRestaurant.setRestroName(jsonObject.getString("RestaurantName"));
我想相应地显示名单.CanterName.can有人告诉我怎么能这样做?
public void requestRestaurantSearchByFilter_list() {
mProgressDialog.show();
StringRequest restrolistrequestfilter = new StringRequest(Request.Method.POST, GlobalData.SEARCHBYFILTERURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
mProgressDialog.dismiss();
mPaymentMethodList.clear();
mDataList.clear();
mAllList.clear();
mAdapter.notifyDataSetChanged();
Log.e("responcefilterlist....", response);
JSONObject jObject = new JSONObject(response);
if (jObject.getString("status").equals("1")) {
JSONArray jsonArray = jObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
PojoRestaurant pojoRestaurant = new PojoRestaurant();
pojoRestaurant.setRestroName(jsonObject.getString("RestaurantName"));
pojoRestaurant.setDeliveryTime(jsonObject.optString("DeliveryTime"));
pojoRestaurant.setPaymentOption(jsonObject.getString("PaymentOptions"));
pojoRestaurant.setMinimumOrder(jsonObject.getString("MinimumOrder"));
if (jsonObject.getString("ImageUrl").equals("") || jsonObject.getString("ImageUrl") == "null") {
} else {
pojoRestaurant.setRestroImage(jsonObject.getString("ImageUrl"));
}
id = jsonObject.getString("ID");
mPaymentMethodList.add(jsonObject.getString("PaymentOptions"));
getpaymentId.add(id);
getListViewId.add(id);
if (jsonObject.getString("OpenStatus").equals("true")) {
pojoRestaurant.setOpenClose("Open");
} else {
pojoRestaurant.setOpenClose("Close");
}
mAllList.add(pojoRestaurant);
mDataList.add(pojoRestaurant);
//mDeliveryList.add(jsonObject.getString("DeliveryTime"));
// mCuisineTypeList.add(jsonObject.getString("RestaurantName"));
}
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("error", "" + volleyError.getMessage());
if (volleyError.getMessage() == null)
requestRestaurantSearchByFilter_list();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String foodid = getArguments().getString("FOODID");
String areaid = getArguments().getString("AREAID");
Log.e("foodid", "" + foodid);
Log.e("areaid", "" + areaid);
params.put("DeliveryAreaID", areaid);
params.put("ProvideOffers", "0");
params.put("NewRestaurant", "0");
params.put("PaymentMethod", "0");
params.put("OpenRestaurant", "0");
params.put("FoodID", foodid);
return params;
}
};
RequestQueue restrolistqueuefilter = Volley.newRequestQueue(getContext());
restrolistrequestfilter.setShouldCache(false);
restrolistqueuefilter.add(restrolistrequestfilter);
}
这是我的Adpater课程
public class RestroListBaseAdapter extends BaseAdapter {
private ArrayList<PojoRestaurant> mList;
private Context mContext;
private ViewHolder viewHolder;
private ArrayList<PojoRestaurant> suggestion = new ArrayList<>();
public RestroListBaseAdapter(Context mContext, ArrayList<PojoRestaurant> mList) {
this.mContext = mContext;
this.mList = mList;
}
@Override
public int getCount() {
if (mList != null) {
return mList.size();
}
return 0;
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int lastPosition = -1;
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.restaurant_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.RestroName = (TextView) convertView.findViewById(R.id.tv_restro_name);
viewHolder.OpenClose = (TextView) convertView.findViewById(R.id.tvOpenClose);
viewHolder.MinimumOrder = (TextView) convertView.findViewById(R.id.tv_minimun_order);
viewHolder.DeliveryTime = (TextView) convertView.findViewById(R.id.tv_delivery_time);
viewHolder.PaymentOption = (TextView) convertView.findViewById(R.id.tv_Payment);
viewHolder.RestroImage = (ImageView) convertView.findViewById(R.id.img_restro_list);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
convertView.startAnimation(animation);
lastPosition = position;
convertView.setTag(viewHolder);
PojoRestaurant pojoRestaurant = (PojoRestaurant) getItem(position);
viewHolder.RestroName.setText(pojoRestaurant.getRestroName());
viewHolder.DeliveryTime.setText(pojoRestaurant.getDeliveryTime());
viewHolder.MinimumOrder.setText(pojoRestaurant.getMinimumOrder());
viewHolder.OpenClose.setText(pojoRestaurant.getOpenClose());
viewHolder.PaymentOption.setText(pojoRestaurant.getPaymentOption());
Picasso.with(mContext).load(pojoRestaurant.getRestroImage()).into(viewHolder.RestroImage);
return convertView;
}
private class ViewHolder {
private TextView RestroName, OpenClose, MinimumOrder, DeliveryTime, PaymentOption;
private ImageView RestroImage;
}
}
答案 0 :(得分:1)
首先,您需要对已经用于设置适配器的列表(取决于您的逻辑,即按字母顺序)进行排序,然后通知列表适配器。
您可以使用自己的比较器对列表进行排序。
Comparator
的示例代码:
Comparator comparator = new Comparator() {
public int compare(PojoRestaurant restro1, PojoRestaurant restro2) {
String restroName1 = restro1.getRestroName.toUpperCase();
String restroName2 = restro2.getRestroName.toUpperCase();
//ascending order
return restroName1.compareTo(restroName2);
//descending order
//return restroName2.compareTo(restroName1);
}
};
然后,您可以使用以下代码对列表进行排序
Collections.sort(yourList, collection)
答案 1 :(得分:1)
您只需按字母顺序对从服务器返回的列表进行排序,然后将此列表直接添加到列表视图中。
例如,您有一个模型类:
public class Restaurant {
String restaurantName;
int restaurantStar;
}
您应该为此类实现Comparator
接口:
public class Restaurant implements Comparator<Restaurant> {
String restaurantName;
int restaurantStar;
@Override
public int compare(Schedule s1, Schedule s2) {
// because class String has already implemented Comparator. just get that
return s1.restaurantName.compare(s2.restaurantName);
}
}
最后,您可以对此餐馆列表进行排序:
// get somewhere on network
List<Restaurant> restaurants;
Collection.sort(restaurants);
最后,您可以直接将此列表分配给listview。例如:
adapter.setData(restaurants);
// refresh listview so listview will display new sorted data
adapter.notifyDataSetChanged();
以下是使用Comparator
的另一个示例:
comparator tutorial