0

This is the method where i am getting the data from the server and show in the listview

I want to show the list alphabetically accordingly to the i am setting the name with the help of model class. pojoRestaurant.setRestroName(jsonObject.getString("RestaurantName"));

I want to show the list accordingly RestaurantName.can anyone tell me how can i do this ??

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);

}

This is my Adpater Class

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;

    }
}
Nikhil
  • 3,711
  • 8
  • 32
  • 43
LoveAndroid
  • 146
  • 1
  • 10

2 Answers2

1

First, you need to sort the list(depending upon your logic i.e. alphabetically) which you have already used to set to the adapter and then notify the list adapter.

You can use your own comparator for sorting the list.

Sample code for 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);
    }

};

Then, you can use following code to sort the list

Collections.sort(yourList, collection)
akshay
  • 5,811
  • 5
  • 39
  • 58
1

You just need to sort that list returned from server by alphabetically order and then add this list directly to your listview.

For example you have a model class:

public class Restaurant {
   String restaurantName;
   int restaurantStar;
}

You should implements Comparator interface for this class:

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);
    }
}

Finally you can sort this list of restaurants:

// get somewhere on network
List<Restaurant> restaurants;
Collection.sort(restaurants);

Finally you can assign directly this list to listview. For example:

adapter.setData(restaurants);
// refresh listview so listview will display new sorted data
adapter.notifyDataSetChanged();

Here is an another example that using Comparator: comparator tutorial

Community
  • 1
  • 1
hqt
  • 29,632
  • 51
  • 171
  • 250