首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有simpleAdapter的Android simpleAdapter不起作用

带有simpleAdapter的Android simpleAdapter不起作用
EN

Stack Overflow用户
提问于 2016-09-10 04:36:09
回答 1查看 505关注 0票数 0

我想使用HashMap和simpleAdapter在Listview中显示一个非常简单的键->值。xml文件包含2个Textview。我之所以使用Hashmap,是因为我需要在单击它们时使用"key“作为引用,然后将其传递给其他活动。

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-10 05:44:40

当给定的布局为行充气时,将根据需要创建在您的SimpleAdapter中使用的idname <TextView>,因此您的布局中不需要idname<TextView>。我们还将<ListView>的宽度和高度更改为match_parent

代码语言:javascript
复制
<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,但是您希望为每个项目显示两个String,所以我们将使用simple_list_item_2。布局中的TextView具有系统资源ID text1text2,因此我们也需要更改to数组。

代码语言:javascript
复制
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);

按照注释中的建议,您已经在构造函数调用中删除了无关的参数,并且现在直接在ListView上调用ListView,所以其他的都应该是好的。

simple_list_item_2会垂直地堆叠它的TextView,所以如果您想将两者都放在一条水平线上,则需要使用您自己的布局。对于一个简单的通用示例,我们将使用一个LinearLayout和两个具有加权宽度的TextView来保持列对齐。

res/layout/文件夹中,list_item.xml

代码语言:javascript
复制
<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>

以及守则中的几处改动:

代码语言:javascript
复制
int[] to = { R.id.text_id, R.id.text_name };

SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.list_item, from, to);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39422600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档