java - ArrayList排序不影响ArrayList

标签 java android arraylist

我试图根据 StringArrayList 进行排序,实际上我在 "" 中有数字,如下所示:"4000"

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

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

这就是我的 JSON 的样子:

{
"locations": [
{
"name": "Office",
"price": "4,00,000"
},
{
"name": "Work",
"price": "1,20,000"
}
]
}

模型类:

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

最佳答案

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

    }

}

关于java - ArrayList排序不影响ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239720/

相关文章:

java - ArrayList 恒定时间和线性时间访问

android - 如何计算无需滚动即可适合 WebView 的文本

java - 覆盖等于

android - 为什么不是所有来自 FirebaseDatabase 引用的数据都添加到 ArrayList<String> 中?

java - list.clear() 与 list = new ArrayList<Integer>();

java - 提取第 n 个字符和另一个第 n 个字符之间的字符串

java - 将 webjar 作为根资源提供服务

java - 如何在 AlertDialog 中添加重新启动

java - 使用 Glide Library 时,小尺寸图像会自动调整大小

java - BufferedWriter.write() 似乎不起作用