按属性对 Listview 中的项目进行排序

Posted

技术标签:

【中文标题】按属性对 Listview 中的项目进行排序【英文标题】:Sorting Items in Listview order by attributes 【发布时间】: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/***RE.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_
    android:layout_
    android:background="#0099cc"
    tools:context="burakkaanerce.turnuvator.ShowTournamentInfo"
    android:orientation="vertical">
    <LinearLayout
        android:layout_
        android:layout_
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        android:layout_weight="1">
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_
            android:layout_ >
            <TextView
                android:id="@+id/titleName"
                android:layout_
                android:layout_
                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_
                android:layout_
                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_
                android:layout_
                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_
                android:layout_
                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_
                android:layout_
                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_
                android:layout_
                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_
                android:layout_
                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_
                android:layout_
                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_
                android:layout_
                android:text="@string/tableP"
                android:layout_weight="1.5"
                android:textStyle="bold"
                android:gravity="left"
                android:layout_gravity="left">
            </TextView>
        </LinearLayout>
        <LinearLayout
            android:layout_
            android:layout_
            android:fitsSystemWindows="true"
            android:orientation="vertical"
            android:layout_weight="1">
            <ScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_
                android:layout_
                android:fillViewport="true">
                <LinearLayout
                    android:layout_
                    android:layout_
                    android:fitsSystemWindows="true">
                    <ListView
                        android:id="@+id/listview"
                        android:layout_
                        android:layout_>
                    </ListView>
                </LinearLayout>
            </ScrollView>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_
            android:layout_ >
            <TextView
                android:id="@+id/fixtureText"
                android:layout_
                android:layout_
                android:text="@string/fixtureText"
                android:layout_weight="1"
                android:textStyle="bold"
                android:gravity="center">
            </TextView>
        </LinearLayout>
        <LinearLayout
            android:layout_
            android:layout_
            android:fitsSystemWindows="true"
            android:orientation="vertical"
            android:layout_weight="1">
            <ScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_
                android:layout_
                android:fillViewport="true">
                <LinearLayout
                    android:layout_
                    android:layout_
                    android:fitsSystemWindows="true">
                    <ListView
                        android:id="@+id/listview2"
                        android:layout_
                        android:layout_>
                    </ListView>
                </LinearLayout>
            </ScrollView>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/fullscreen_content_controls" style="?metaButtonBarStyle"
        android:layout_
        android:layout_
        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_
            android:layout_
            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 【参考方案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.sortx,y->
  if(x.clause1 == y.clause1)
    x.clause2<=> y.clause2
  else
    x.clause1<=> y.clause1
  

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

【讨论】:

@burakkaanerce 伙计,你只是说它是按 id 打印的,而不是你想怎么做的:P 反正我编辑过,检查一下 老兄,你确定吗?我找不到这些方法。 @burakkaanerce 抱歉,我对上次的编辑感到困惑,我现在修复了它,它应该可以工作了!

以上是关于按属性对 Listview 中的项目进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中按数值列对listview项目进行排序

按数据属性对列表排序

按特定值对 ListView 或 ListView.ItemTemplate 进行排序

Android 按字母顺序对 ListView 进行排序

如何在 Visual Basic 6 ListView 上进行多列排序?

如何使用 listview 项目 android 对 listview 进行排序?