Search code examples
androidandroid-listviewbaseadapterlistadapter

Sorting Items in Listview order by attributes


I have been trying to make League Table. I have 2 questions about the program.

First one is sorting players in table. All codes are working, getting players and matches, calculating scores and average with all matches which is players played. But in league table, it prints the players order by id, I want it to do order by points and average. How can I do this ?

Here is PlayerListAdapter Class:

public class PlayerListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<Player> playerList;

    public PlayerListAdapter(Activity activity, List<Player> players) {
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        playerList = players;
    }

    @Override
    public int getCount() {
        return playerList.size();
    }

    @Override
    public Object getItem(int position) {
        return playerList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.textview_rowplayer, null);

        TextView textView = (TextView) vi.findViewById(R.id.FirstText);
        TextView textView2 = (TextView) vi.findViewById(R.id.SecondText);
        TextView textView3 = (TextView) vi.findViewById(R.id.ThirdText);
        TextView textView4 = (TextView) vi.findViewById(R.id.FourthText);
        TextView textView5 = (TextView) vi.findViewById(R.id.FifthText);
        TextView textView6 = (TextView) vi.findViewById(R.id.SixthText);
        TextView textView7 = (TextView) vi.findViewById(R.id.SeventhText);
        TextView textView8 = (TextView) vi.findViewById(R.id.EightText);
        TextView textView9 = (TextView) vi.findViewById(R.id.NinthText);

        final Player player = playerList.get(position);

        List<Match> matches = dbHelper.getAllMatches(player.getId(), player.gettID());

        int totalMatch = 0;
        int totalWin = 0;
        int totalDraw = 0;
        int totalLose = 0;
        int totalScore = 0;
        int totalGoal = 0;
        int totalAverage = 0;
        int totalPoint = 0;

        for (int i = 0; i < matches.size(); i++) {
            Match match = matches.get(i);
            if(match.get_played() == 1) {
                totalMatch++;

                int score = match.get_fScore() - match.get_sScore();

                if(match.get_fPID() == player.getId()) {
                    if(score > 0) {
                        totalWin++;
                        totalPoint += 3;
                    } else if(score == 0) {
                        totalDraw++;
                        totalPoint += 1;
                    } else {
                        totalLose++;
                    }
                    totalScore += match.get_fScore();
                    totalGoal += match.get_sScore();
                    totalAverage += score;
                } else if (match.get_sPID() == player.getId()) {
                    if(score < 0) {
                        totalWin++;
                        totalPoint += 3;
                    } else if(score == 0) {
                        totalDraw++;
                        totalPoint += 1;
                    } else {
                        totalLose++;
                    }
                    totalScore += match.get_sScore();
                    totalGoal += match.get_fScore();
                    totalAverage -= score;
                } else {
                    totalMatch--;
                    continue;
                }
            } else {
                continue;
            }
        }

        textView.setText(player.getName());
        textView2.setText(String.valueOf(totalMatch));
        textView3.setText(String.valueOf(totalWin));
        textView4.setText(String.valueOf(totalDraw));
        textView5.setText(String.valueOf(totalLose));
        textView6.setText(String.valueOf(totalScore));
        textView7.setText(String.valueOf(totalGoal));
        textView8.setText(String.valueOf(totalAverage));
        textView9.setText(String.valueOf(totalPoint));

        return vi;
    }
}

Here is SS: https://i.sstatic.net/vpNRE.jpg

The second question is that, how can I remove blank between table and fixture ? I put ScrollView in table and fixture, because if player count is more than 15 or 16 table will push the fixture to bottom of program. Same as the fixture. Now it is working right but I want to make more beautiful design.

Here is activity_show_tournament_info.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context="burakkaanerce.turnuvator.ShowTournamentInfo"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        android:layout_weight="1">
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/titleName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/teamName"
                android:layout_weight="1"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titlePlayed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableO"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titleWin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableG"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titleDraw"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableB"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titleLose"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableM"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titleScored"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableA"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titleGoaled"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableY"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titleAverage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableAV"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/titlePoint"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tableP"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:orientation="vertical"
            android:layout_weight="1">
            <ScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:fillViewport="true">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true">
                    <ListView
                        android:id="@+id/listview"
                        android:layout_height="match_parent"
                        android:layout_width="match_parent">
                    </ListView>
                </LinearLayout>
            </ScrollView>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/fixtureText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/fixtureText"
                android:layout_weight="1"
                android:textStyle="bold"
                android:gravity="center">
            </TextView>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:orientation="vertical"
            android:layout_weight="1">
            <ScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:fillViewport="true">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true">
                    <ListView
                        android:id="@+id/listview2"
                        android:layout_height="match_parent"
                        android:layout_width="match_parent">
                    </ListView>
                </LinearLayout>
            </ScrollView>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/fullscreen_content_controls" style="?metaButtonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:orientation="horizontal"
        tools:ignore="UselessParent">

        <Button
            android:id="@+id/add_match" style="?metaButtonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/add_match"
            android:onClick="add_match"/>
    </LinearLayout>
</LinearLayout>

Player Class:

package burakkaanerce.turnuvator;

public class Player {
    private int _id;
    private String _name;
    private int _tID;

    public Player(String _name, int _tID) {
        super();
        this._name = _name;
        this._tID = _tID;
    }

    public Player() {
        super();
    }

    public int getId() {
        return _id;
    }

    public void setId(int _id) {
        this._id = _id;
    }

    public String getName() {
        return _name;
    }

    public void setName(String _name) {
        this._name = _name;
    }

    public int gettID() {
        return _tID;
    }

    public void settID(int _tID) {
        this._tID = _tID;
    }

}

Solution

  • I did it with Comparator. It is sorting order by points. But I have a problem. If 2 player have same points, how can I sort them order by averages ?

    PointSorter.java

    package burakkaanerce.turnuvator;
    
    import java.util.Comparator;
    
    public class PointSorter implements Comparator<Player> {
        public int compare(Player aPlayer, Player anotherPlayer) {
            int returnVal = 0;
    
            if(aPlayer.get_totalPoint() > anotherPlayer.get_totalPoint()){
                returnVal =  -1;
            }else if(aPlayer.get_totalPoint() < anotherPlayer.get_totalPoint()){
                returnVal =  1;
            }else if(aPlayer.get_totalPoint() == anotherPlayer.get_totalPoint()){
                returnVal =  0;
            }
            return returnVal;
        }
    }
    

    Calling sort:

    List<Player> players = dbHelper.getAllPlayers(temptID);
    Collections.sort(players, new PointSorter());