排序对象的ArrayList

时间:2018-12-13 04:05:23

标签: java android

嘿,所有的stackoverflowers。第二天在这里,发现它非常有帮助和友好。我正在做一个简单的android应用程序。我创建了一个具有不同属性(名称,年龄,高度,位置,点和图像)的Player对象。现在,我需要通过EditText根据输入的积分值对玩家列表进行排序。据我了解,我需要使用Collections.sort方法。但是我不知道如何获取输入的值并将其放入Arraylist。有人可以给我提示如何做吗?

这是我的代码。

MainActivity

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

List<Player> bostonCeltics = new ArrayList<>();
ListView listView;

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

    listView = (ListView) findViewById(R.id.listView);
    listView.setOnItemClickListener(this);

    bostonCeltics.add(new Player("Kyrie Irving", "23-Mar-92", 190.5, "Point guard", R.mipmap.irving, 0));
    bostonCeltics.add(new Player("Jayson Tatum", "3-Mar-98", 190.5, "Small forward", R.mipmap.tatum, 0));
    bostonCeltics.add(new Player("Gordon Hayward", "23-Mar-90", 203.2, "Small forward", R.mipmap.hayward, 0));
    bostonCeltics.add(new Player("Robert Williams", "17-Oct-97", 208.3, "Power forward", R.mipmap.williams, 0));
    bostonCeltics.add(new Player("Jabari Bird", "3-Jul-94", 198.1, "Shooting guard", R.mipmap.bird, 0));
    bostonCeltics.add(new Player("Al Horford", "3-Jun-86", 208.3, "Center / Power forward", R.mipmap.horford, 0));
    bostonCeltics.add(new Player("Jaylen Brown", "24-Oct-96", 200.7, "Small forward/ Shooting guard", R.mipmap.brown, 0));
    bostonCeltics.add(new Player("Marcus Smart", "6-Mar-94", 193.0, "Shooting guard / Point guard", R.mipmap.smart, 0));
    bostonCeltics.add(new Player("Terry Rozier", "17-Mar-94", 185.4, "Point guard", R.mipmap.rozier, 0));
    bostonCeltics.add(new Player("Aron Baynes", "9-Dec-86", 208.3, "Center / Power forward", R.mipmap.baynes, 0));
    bostonCeltics.add(new Player("Marcus Morris", "2-Sep-89", 205.7, "Forward", R.mipmap.morris, 0));
    bostonCeltics.add(new Player("Brad Wanamaker", "25-Jul-89", 193.0, "Point guard / Shooting guard", R.mipmap.wanamaker, 0));
    bostonCeltics.add(new Player("P. J. Dozier", "25-Oct-96", 198.1, "Shooting guard", R.mipmap.dozier, 0));
    bostonCeltics.add(new Player("Daniel Theis", "4-Apr-92", 203.2, "Center / Power forward", R.mipmap.theis, 0));
    bostonCeltics.add(new Player("Guerschon Yabusele", "17-Dec-95", 203.2, "Power forward", R.mipmap.yabusele, 0));
    bostonCeltics.add(new Player("Semi Ojeleye", "5-Dec-94", 200.7, "Forward", R.mipmap.ojeleye, 0));
    bostonCeltics.add(new Player("Walt Lemon Jr.", "26-Jul-92", 190.5, "Point guard", R.mipmap.lemon, 0));
    System.out.println("before sorting %%% " + bostonCeltics);
    Collections.sort(bostonCeltics);
    for(Player player:bostonCeltics) {
        System.out.println("Sorted players are " + player.getName());
    }

    TestAdapter adapter = new TestAdapter(this, bostonCeltics);
    listView.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent moveToDetailIntent = new Intent(getBaseContext(), PlayerDetailActivity.class);
    moveToDetailIntent.putExtra("player", bostonCeltics.get(position));
    startActivity(moveToDetailIntent);
}}

玩家类

public class Player implements Serializable {
private String name;
private String age;
private double height;
private String position;
private int image;
private int points;
Player(String name, String age, double height, String position, int image, int points) {
    this.name = name;
    this.age = age;
    this.height = height;
    this.position = position;
    this.image = image;
    this.points = points;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}
public double getHeight() {
    return height;
}
public void setHeight(double height) {
    this.height = height;
}
public String getPosition() {
    return position;
}
public void setPosition(String position) {
    this.position = position;
}
public int getImage() {
    return image;
}
public void setImage(int image) {
    this.image = image;
}
public int getPoints() { return points; }
public void setPoints(int points) { this.points = points; }}

PlayerDetailActivity

public class PlayerDetailActivity extends AppCompatActivity {

TextView nameTextView;
TextView ageTextView;
TextView heightTextView;
TextView positionTextView;
ImageView imageView;
EditText pointsEditText;

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

    nameTextView = (TextView) findViewById(R.id.nameTextView);
    ageTextView = (TextView) findViewById(R.id.ageTextView);
    heightTextView = (TextView) findViewById(R.id.heightTextView);
    positionTextView = (TextView) findViewById(R.id.positionTextView);
    imageView = (ImageView) findViewById(R.id.imageView);
    pointsEditText = (EditText) findViewById(R.id.pointsEditText);

    Player player = (Player) getIntent().getSerializableExtra("player");

    nameTextView.setText(player.getName());
    ageTextView.setText("Birth date: " + player.getAge());
    heightTextView.setText("Height: " + player.getHeight());
    positionTextView.setText("Position: " + player.getPosition());
    imageView.setImageResource(player.getImage()) }}

感谢您的时间和帮助。

2 个答案:

答案 0 :(得分:4)

您还可以使用Collections.sort()方法通过传递自定义比较器对象来对数组列表进行排序

Collections.sort(bostonCeltics, Comparator.comparingInt(Player::getPoints));

答案 1 :(得分:1)

您可以使用Comparator.comparing,这将根据用户点对List进行排序

List<Player> players = new ArrayList<>();
players.sort(Comparator.comparing(Player::getPoints));