I am trying to sort ArrayList on the basis of String actually I have numbers in "", like this: "4000"
I have written code to sort arraylist on the basis of price, but whenever i do tap on button it is not affecting to my ListView... why ?
I am following this tutorial....
This is how my JSON looks:
{
"locations": [
{
"name": "Office",
"price": "4,00,000"
},
{
"name": "Work",
"price": "1,20,000"
}
]
}
Model class:
public class Locations {
private String name;
private String price;
public Locations() {
// TODO Auto-generated constructor stub
}
public Locations(String name, String price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
LocationsActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locations);
actorsList = new ArrayList<Locations>();
new JSONAsyncTask().execute(" ");
listview = (ListView) findViewById(R.id.list);
adapter = new LocationsAdapter(getApplicationContext(), R.layout.adapter_locations, actorsList);
btnHTL = (Button) findViewById(R.id.btnHTL);
btnHTL.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Collections.sort(actorsList, new Comparator<Locations>(){
public int compare(Locations obj1, Locations obj2)
{
// ascending
return (Integer)(arg1.price).compareTo(arg2.price);
}
});
}
});
btnLTH = (Button) findViewById(R.id.btnLTH);
btnLTH.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Collections.sort(actorsList, new Comparator<Locations>(){
public int compare(Locations obj1, Locations obj2)
{
// descending
return (Integer)(arg2.price).compareTo(arg1.price);
}
});
}
});
}