0

I am building a simple Android app. I created a Player object with different attributes (name, age, height, position, points, and images). Now I need to sort list of Players according to entered value of points, through EditText. As I understood I need to use Collections.sort method. But I don't know how to get entered values and place it into an Arraylist. Could someone give me a tips how to do it?

Here is my code.

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

Player Class

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

Thank you for your time and help..

2
  • 1
    What did you try so far to sort it? Commented Dec 13, 2018 at 4:06
  • 1
    before you can sort your players, you need to actually update your objects to include the points. Which means that you should probably be storing your players somewhere that doesn't disappear when your activity is killed (which can happen anytime it is not on screen). For starters, you can consider a static variable pretty much anywhere, but soon enough you'll want to look at more serious storing options (like sqlite for example) Commented Dec 13, 2018 at 4:13

2 Answers 2

4

You can also use Collections.sort() method to sort the arraylist by passing custom comparator object

Collections.sort(bostonCeltics, Comparator.comparingInt(Player::getPoints));
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Comparator.comparing this will sort the List depending on user points

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.