Java 使用HashMap使用自定义desjgn填充ListView
我在活动MyBets中有一个列表视图。XML布局如下:Java 使用HashMap使用自定义desjgn填充ListView,java,android,listview,android-listview,custom-lists,Java,Android,Listview,Android Listview,Custom Lists,我在活动MyBets中有一个列表视图。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="@di
<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>
我使用了下面的代码来获取我想要添加的团队和选择变量
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);
我的问题是如何填充listview,以便团队名称和选择显示团队和选择。我从一个名为AllGameslist的独立类调用此方法。声明一个扩展适配器的类。通过inflater的帮助定义自定义视图,然后在Map/List的帮助下设置要从活动/片段传递到适配器类的值(如果迭代)。假设您正在以json格式检索用户的详细信息,如[{“用户名”:“StackOverflow”,“密码”:“xxxxxx”,“created”:“08 Apr 02:30 2015 PM”,“id:“15014”},{“用户名”:“StackExchange”,“密码”:“xxxxxxxx”,“已创建”:“09 Apr 02:30 2015 PM”,“id:“15015”}] 现在,您希望在列表视图中显示它。 1) 提取此json并在类似bean的user.setName(jsonObject.getString(“name”))中设置此值,依此类推。然后将该对象添加到ArrayList.add(user)中。 2) 迭代所有数据项并在最后一次中填充它们之后,将列表传递给您定义的适配器。 3) 在适配器中,在getView()方法中,按您希望的方式设置listview项并向用户显示。您还可以为用户选择的项实现onItemClick。
作为进一步的参考:你能写一个简短的代码来演示我将如何做吗。
Teams Selection Status
A-B A @ 2.3 Open
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