【问题标题】:Populating ListView with Custom desjgn using HashMap使用 HashMap 使用自定义设计填充 ListView
【发布时间】: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


    【解决方案1】:

    声明一个扩展适配器的类。通过 inflater 的帮助定义自定义视图,然后在 Map/List 的帮助下设置要设置的值(如果迭代),您从活动/片段传递到适配器类。

    【讨论】:

    • 你能写一个简短的代码来演示我会怎么做吗?
    【解决方案2】:

    假设您正在以 json 格式检索用户的详细信息,例如 [{"username":"StackOverflow","password":"xxxxxx","created":"08 Apr 02:30 2015 PM","id ":"15014"},{"username":"StackExchange","password":"xxxxxxxx","created":"09 Apr 02:30 2015 2015 PM","id":"15015"}] 现在,您想在列表视图中显示它。 1)提取此json并将此值设置在像user.setName(jsonObject.getString(“name”))这样的bean中;等等。然后将该对象添加到 ArrayList list.add(user) 中。 2)在遍历所有数据项并在最后一次将列表中填充它们之后,将列表传递给您定义的适配器。 3) 在适配器中,在 getView() 方法中以您想要的方式设置列表视图项目并显示给用户。您还可以为用户选择的项目实现 onItemClick。 进一步参考:https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView

    【讨论】:

      猜你喜欢
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      相关资源
      最近更新 更多