排序ArrayList不会影响ArrayList

时间:2014-08-11 09:15:31

标签: java android arraylist

我试图在ArrayList String的基础上对""进行排序实际上我在"4000"中有数字,如下所示:JSON

我已经编写了基于价格对arraylist进行排序的代码,但每当我按下按钮时它都不会影响我的ListView ...为什么?

我正在关注this教程......

这就是我的{ "locations": [ { "name": "Office", "price": "4,00,000" }, { "name": "Work", "price": "1,20,000" } ] } 看起来的样子:

Model

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

{{1}}

5 个答案:

答案 0 :(得分:1)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button asc = (Button) findViewById(R.id.asc);
        Button desc = (Button) findViewById(R.id.desc);
        final ArrayList<Location> actorsList = new ArrayList<Location>();

        Location loc = new Location();
        loc.setName("1");
        loc.setPrice("4000");
        actorsList.add(loc);

        loc = new Location();
        loc.setName("2");
        loc.setPrice("8000");
        actorsList.add(loc);

        loc = new Location();
        loc.setName("3");
        loc.setPrice("1000");
        actorsList.add(loc);

        loc = new Location();
        loc.setName("4");
        loc.setPrice("16000");
        actorsList.add(loc);

        asc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Collections.sort(actorsList, new Comparator<Location>() {
                    @Override
                    public int compare(Location arg1, Location arg2) {
                        Integer obj1 = new Integer(arg1.getPrice().replace(",",""));
                        Integer obj2 = new Integer(arg2.getPrice().replace(",",""));
                        return (Integer) (obj1).compareTo(obj2);
                    }
                });

                for (int i = 0; i < actorsList.size(); i++) {
                    System.out.println("monika 1233"
                            + actorsList.get(i).getPrice());
                }
            }
        });

    }

}

答案 1 :(得分:0)

您可以使用Collections.sort,它在参数中使用比较器。在比较器中,您将定义要比较的内容(此处为价格)。

答案 2 :(得分:0)

我添加了int排序代码:

Collections.sort(actorsList, new Comparator<Locations>() {
    public int compare(Locations user1, Locations user2) {
        return user1.price.compareTo(user2.price);
    }
});

implement模型中的Comparable Location方法:

Locations implements Comparable<Locations>

答案 3 :(得分:0)

像这样上课:

class PriceSort implements Comparator<location> {
    @Override
    public int compare(location cr1, location cr2) {

          long l1 = Integer.parseInt(cr1.getPrice());
          long l2 = Integer.parseInt(cr2.getPrice());
          return Long.compare(l1, l2);

    }
}

并致电:

Collections.sort(actorsList, new PriceSort());

答案 4 :(得分:0)

如果您只想按价格对列表进行排序,请使用自定义comparator。如果您想根据任何搜索字词过滤列表,请使用Filterable interface