LoveAndroid LoveAndroid - 2 months ago 7
Android Question

How to get the selected item id in the listview while listview is sorted according to the name?

I am getting data from the json.and showing in the listview. i sorted the listview according to the name using Comparator.The problem is that when i click on the sorted listview it's getting the id of first item not the id of the sorted item.can anyone tell me how can i solve this problem ??

This is my json data :-

{
"status": 1,
"StatusMessage": "Restaurent List",
"data": [
{
"ID": "7",
"RestaurantName": "Water Lemon",
"MinimumOrder": "20.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},
{
"ID": "8",
"RestaurantName": "Casper and Gambini",
"MinimumOrder": "40.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},
{
"ID": "11",
"RestaurantName": "B L E N D S",
"MinimumOrder": "50.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},
{
"ID": "12",
"RestaurantName": "khyber Indian Cuisine",
"MinimumOrder": "5.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},
{
"ID": "13",
"RestaurantName": "Khyber Indian",
"MinimumOrder": "5.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},
{
"ID": "14",
"RestaurantName": "test",
"MinimumOrder": "10.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},
{
"ID": "15",
"RestaurantName": "yahoo",
"MinimumOrder": "5.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},
{
"ID": "16",
"RestaurantName": "Khaber",
"MinimumOrder": "3.000",
"DeliveryTime": "",
"OpenStatus": true,
"PaymentOptions": "VISA, MasterCard , Maestro , American Express",
"Is New Restaurant": true,
"Is Provide Offer": true
},


This is the method() Where i parsing the json and showing in the listview :-

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



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

pojoRestaurant.setRestroId(id);

mPaymentMethodList.add(jsonObject.getString("PaymentOptions"));


if (jsonObject.getString("OpenStatus").equals("true")) {

pojoRestaurant.setOpenClose("Open");


} else {
pojoRestaurant.setOpenClose("Close");


}

mAllList.add(pojoRestaurant);
mDataList.add(pojoRestaurant);

Collections.sort(mDataList, RestaurantList.getCompByName());

getpaymentId.add(id);
getListViewId.add(id);
}

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



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 Comparator method() by which i set the listview accordingly to the restro name. :-

public static Comparator<PojoRestaurant> getCompByName() {
Comparator<PojoRestaurant> comp = new Comparator<PojoRestaurant>() {
@Override
public int compare(PojoRestaurant lhs, PojoRestaurant rhs) {
return lhs.RestroName.compareTo(rhs.RestroName);
}
};
return comp;
}


The following is the on click listner on the listview where i get the position of the listview item and get the id of the listview item :-

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mListViewItemId = getListViewId.get(i);
Fragment fragment = new RestrurantName();
Bundle b = new Bundle();
b.putString("ListViewitemId", mListViewItemId);

fragment.setArguments(b);


getFragmentManager().beginTransaction().replace(R.id.frame, fragment).addToBackStack(null).commit();
toolbar.setTitle("Restaurant Name");

}
});


Where getListViewId is an array list of the string type.

This is my Adapter class :-

public class RestroListBaseAdapter extends BaseAdapter {

private ArrayList<PojoRestaurant> mList;
private Context mContext;
private ViewHolder viewHolder;


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;

}


}


Problem :-
When i select the item from the listview the id (mListViewItemId )is taking accordingly to the json id. I want when i select the particular item it takes the id of that item.
In my case when i select Restro name (B L E N D S) it taking the id 7.please see my json and tell me how can i do this ??

Answer

From your code I can see that you have sorted the mDataList arraylist to get the sorted list. But you have stored the list of id in separate list which is getListViewId that you have not sorted.

If you sort your mDataList then you also have to sort your getListViewId list to get the proper list.

But i would suggest you to get the list item id from your mDataList only as below

    mListViewItemId = mDataList.get(i).getRestroId();

Hope this will guide you. Let me know if any query.

Comments