【问题标题】:Android HashMap with simpleAdapter is not working带有 simpleAdapter 的 Android HashMap 不起作用
【发布时间】:2016-09-10 14:16:53
【问题描述】:

我想通过使用HashMap和simpleAdapter在Listview中显示一个非常简单的Key->value。 xml 文件包含 2 个 Textview。我使用 Hashmap 的原因是因为我需要在单击它们时使用“key”作为参考,然后将其传递给另一个 Activity。

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    String[] animal_id = new String[] {
            "1",
            "2",
            "3"

    };
    String[] animal_names = new String[] {
            "mouse",
            "dragon",
            "tiger"
    };

    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    for(int i=0;i<3;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("ID",animal_id[i]);
        hm.put("Name",animal_names[i]);
        aList.add(hm);
    }

    String[] from = { "ID","Name"};

    int[] to = { R.id.id, R.id.name};

    SimpleAdapter adapter = new SimpleAdapter(this, aList, android.R.layout.simple_list_item_1, from, to);

    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);



}

XML

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.volley.Main2Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:id="@+id/id"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name"
        android:id="@+id/name"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />
</RelativeLayout>

【问题讨论】:

  • SimpleAdapter 构造函数调用中有一个额外的参数。删除getBaseContext()
  • 作为@MikeM。说,您在构造函数中有一个额外的参数,要修复它,只需将 this, getbaseContext() 替换为 getApplicationContext() 即可。
  • @MikeM。谢谢。但是这一行“setListAdapter(adapter);”仍然是红色。任何想法 ?看起来我缺少一些编码才能在列表视图中显示它?
  • 你在AppCompatActivity,而不是ListActivity。没有setListAdapter() 方法。您需要直接在您的ListView 上致电setAdapter()
  • 首先,您通常不希望wrap_content 获得ListView。我猜你的布局中并不真的需要那些&lt;TextView&gt;s,所以你可以删除它们,并让你的ListViewmatch_parent双向。然后,您可能希望使用android.R.layout.simple_list_item_2 代替Adapter,因为您似乎希望在每个项目中有两个东西,而您的to 数组将是{android.R.id.text1, android.R.id.text2}

标签: android


【解决方案1】:

SimpleAdapter 中使用的 TextViews 将在给定布局为行膨胀时根据需要创建,因此您不需要 idname &lt;TextView&gt;s你的布局。我们还将&lt;ListView&gt; 的宽度和高度更改为match_parent

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.volley.Main2Activity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView" />

</RelativeLayout>

然后,您提供的布局 SimpleAdapter - simple_list_item_1 - 只是一个 TextView,但您想为每个项目显示两个 Strings,所以我们将使用 simple_list_item_2反而。该布局中的 TextViews 具有系统资源 ID text1text2,因此我们还需要更改您的 to 数组。

String[] from = { "ID", "Name" };
int[] to = { android.R.id.text1, android.R.id.text2 };

SimpleAdapter adapter = new SimpleAdapter(this, aList, android.R.layout.simple_list_item_2, from, to);

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);

按照 cmets 中的建议,您已经在构造函数调用中删除了无关参数,现在您正在直接在您的 ListView 上调用 setAdapter(),所以其他一切都应该很好。


simple_list_item_2 确实将其TextViews 垂直堆叠,因此如果您希望将两者放在一条水平线上,则需要使用自己的布局。对于一个简单的通用示例,我们将使用一个 LinearLayout 和两个具有加权宽度的 TextViews,以保持列对齐。

在您的res/layout/ 文件夹中,list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/text_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

</LinearLayout>

以及代码中的一些改动:

int[] to = { R.id.text_id, R.id.text_name };

SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.list_item, from, to);

【讨论】:

  • 太棒了!您的编码在一行中显示 2 行。对于我的示例,实际上我想在一行中的两列中显示它。这意味着每一行,左侧显示ID,右侧显示名称。如果要这样做,是否必须为其创建另一个 xml 模板?或者您需要我发布另一个问题以便您从那里回答吗?
  • 是的,在这种情况下,您必须创建自己的 XML 布局来提供 Adapter,而不是 android.R.layout。系统提供的那些非常通用,而且种类有限。一个简单的 LinearLayout 和两个 TextViews 可以满足您的需求。您需要确保再次更改to 数组,以使用您的TextViews R.ids,而不是android.R.ids。我可以在这个答案中添加一些简单的例子。给我一分钟。
  • 非常感谢。我将使用 onclick 项目并希望它顺利,如果不是我会在 30 分钟内将其发布到 SOF 上。如果您还在附近,请随意看看 :)
  • 嘿迈克,我发布了一些复杂的东西,看看你有没有时间看看?问题在这里 -> stackoverflow.com/questions/39424112/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多