【问题标题】:Sorting Items in Listview order by attributes按属性对 Listview 中的项目进行排序
【发布时间】:2015-11-20 14:53:52
【问题描述】:

我一直在努力上榜。我有 2 个关于该计划的问题。

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

这是 PlayerListAdapter 类:

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 放在表格和夹具中,因为如果玩家人数超过 15 或 16 人,表格会将夹具推到程序的底部。与夹具相同。现在它工作正常,但我想做更漂亮的设计。

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

玩家类:

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

}

【问题讨论】:

  • 请添加Player类代码。
  • 你试过使用Filterable接口吗?
  • 编辑主帖@petey

标签: android android-listview baseadapter listadapter


【解决方案1】:

我是用 Comparator 做的。它是按点排序的。但我有一个问题。如果 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());

【讨论】:

    【解决方案2】:
    • 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
      • 您可以在滚动视图中放置列表

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

    EDIT2:

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

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

    【讨论】:

    • @burakkaanerce 伙计,你只是说它是按 id 打印的,而不是你想怎么做的:P 反正我编辑过,检查一下
    • 老兄,你确定吗?我找不到这些方法。
    • @burakkaanerce 抱歉,我对上次的编辑感到困惑,我现在修复了它,它应该可以工作了!
    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多