按属性

时间:2015-08-26 14:00:11

标签: android android-listview baseadapter listadapter

我一直在尝试制作联赛表。我有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

第二个问题是,如何在桌子和夹具之间移除空白?我把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;
    }

}

2 个答案:

答案 0 :(得分:0)

  • 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
  }
}
抱歉,我错过了最后的编辑,很困惑

答案 1 :(得分:0)

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