按属性对列表视图中的项目进行排序

Sorting Items in Listview order by attributes

我一直在努力打联赛Table。我有 2 个关于该计划的问题。

第一个是 table 中的球员排序。所有代码都在工作,获取球员和比赛,计算球员参加的所有比赛的得分和平均值。但是在联赛 table 中,它按 id 打印球员顺序,我希望它按积分和平均值 排序。我该怎么做?

这是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;
    }
}

这里是SS: http://i.stack.imgur.com/vpNRE.jpg

第二个问题是,如何去除table和fixture之间的空白?我将 ScrollView 放在 table 和 fixture 中,因为如果玩家数量超过 15 或 16 table 会将 fixture 推到程序的底部。与夹具相同。现在可以正常使用了,但我想做更漂亮的设计。

这里是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>

玩家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;
    }

}
  • 1) 您可以使用此方法对项目进行排序:

    playerList.Sort((x, y) => string.Compare(x.Name, y.Name));
    //or however playerList and name are called ;P
    
  • 2) 我看代码迷路了,所以我告诉你怎么做:

    • LinearLayout 容器填满整个屏幕;
    • 两个带有 with="match parent"heigth="wrap content"
    • 的 ScrollView
    • 在滚动视图中,您可以放置​​列表

如果你有疑问或者你认为我说错了什么,请告诉我:)

编辑 2:

playerList.sort{x,y->
  if(x.clause1 == y.clause1){
    x.clause2<=> y.clause2
  }else{
    x.clause1<=> y.clause1
  }
}

对不起,我写错了最后的编辑,很困惑

我是用比较器做的。它是按点排序。但我有一个问题。如果 2 位玩家的点数相同,我如何按平均数对他们进行排序?

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

调用排序:

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