【发布时间】:2015-05-23 15:09:03
【问题描述】:
我在 MyBets 活动中有一个 ListView。 XML 布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background = "@color/grey05"
tools:context="com.example.betterapp.MyBets">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:background = "@color/betcolor"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
</RelativeLayout>
我在活动 SingleBet 中也有此 ListView 的自定义布局。 XML 布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#49DE56"
android:orientation="horizontal" >
<TextView
android:id="@+id/namesofteams"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="1.3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Sevilla vs Barcelona"
android:textStyle="bold" />
<TextView
android:id="@+id/selection"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="40dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:textColor= "#585E5E"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Sevilla 3.55"
android:textStyle="italic" />
<TextView
android:id="@+id/statusofbet"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Open"
android:textStyle="bold" />
</LinearLayout>
自定义布局应该如下所示:
Teams Selection Status
A-B A @ 2.3 Open
我已使用以下代码来获取我想添加的 Teams 和 Selection 变量。
HashMap<Integer,Game> hashmapofgames = new HashMap<Integer, Game>();
HashMap<Integer, Game> listofgames;
listofgames = newBet.getlistofgames();
for (Integer x : listofgames.keySet()) {
TextView teams = (TextView) findViewById(R.id.textView2);
String getTeams = listofgames.get(x).getTeams();
teams.setText(getTeams);
TextView selectionodds = (TextView) findViewById(R.id.textView3);
String selectedoutcome = listofgames.get(x).getSelection();
Double selectedoutcomeodds = listofgames.get(x).getSelectedOdds();
String selectedteam = "";
if (selectedoutcome == "home") {
selectedteam = getTeams.substring(0, getTeams.lastIndexOf("-"));
} else if (selectedoutcome == "draw") {
selectedteam = "Draw";
} else if (selectedoutcome == "away") {
selectedteam = getTeams.substring(getTeams.lastIndexOf("- ")+1,getTeams.length());
}
String selection = (selectedteam + " @" + selectedoutcomeodds);
Log.d("teams",getTeams);
Log.d("selected odds",Double.toString(selectedoutcomeodds));
Log.d("selected team", selectedteam);
Log.d("selected outcome", selectedoutcome);
Log.d("selection", selection);
这工作正常,因为日志的输出显示:
29523-29523/com.example.betterapp D/teams﹕ Villareal - Valencia
29523-29523/com.example.betterapp D/selected odds﹕ 2.5
29523-29523/com.example.betterapp D/selected team﹕ Valencia
29523-29523/com.example.betterapp D/selected outcome﹕ away
29523-29523/com.example.betterapp D/selection﹕ Valencia @2.5
我的问题是如何填充列表视图,以便团队名称和选择显示团队和选择。我从一个名为 AllGameslist 的单独类中调用此方法。
【问题讨论】:
标签: java android listview android-listview custom-lists