6

我在将 arraylist 添加到列表视图时遇到问题,将在此处解释我的问题.. 告诉我这里出了什么问题... 我有三个线性布局,在中间布局中我有列表视图,如下面的 xml 文件所示。 .`

 <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.59"
        android:src="@drawable/x_title" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout_grouplist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.09"
    android:orientation="vertical"
    android:weightSum="1" >

    <ListView
        android:id="@+id/listview_grouplist"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.39"
        android:text="TextView" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
  </LinearLayout>
</LinearLayout>`

当我尝试使用数组适配器将项目添加到列表时,它对我来说工作正常..但它不适用于列表适配器。它正在崩溃。

ListAdapter adapter = new SimpleAdapter(this,
      mylist, 
      R.layout.grouplistlayout, 
      new String[] { "Group Name" }, 
      new int[] { R.id.listview_grouplist });

String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
      "Linux", "OS/2" };

ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
lst_projlist.setAdapter(adapter1);

有人可以告诉我这里缺少什么吗?

4

4 回答 4

5

你的问题在这里:

    R.layout.grouplistlayout,
    new int[] { R.id.listview_grouplist }); 

顶部应该指向列表布局,例如android.R.layout.simple_list_item_1 底部应该指向 TextView,而不是列表视图。

另外,我看不到您将适配器绑定到列表视图的位置。

来自SimpleAdapter 文档

public SimpleAdapter(Context context, 
                     List<? extends Map<String, ?>> data, 
                     int resource, 
                     String[] from, 
                     int[] to)

自:API 级别 1

构造函数

参数
context 与此 SimpleAdapter 关联的 View 正在运行
数据的上下文 A List of Maps。列表中的每个条目对应于列表中的一行。Maps 包含每一行的数据,并且应该包括定义此列表项的视图的视图布局的“来自”
资源资源标识符中指定的所有条目。 布局文件应该至少包含那些在“to”中定义的命名视图,这些命名视图
被添加到与每个项目关联的 Map 中。
to 应该在“from”参数中显示列的视图。这些都应该是 TextViews。此列表中的前 N ​​个视图被赋予 from 参数中前 N 列的值。

于 2012-04-11T04:50:33.160 回答
1

如果您使用 Activity 进行扩展,那么您必须使用 setAdapter,如果您使用 ListActivity 进行扩展,那么您应该使用 setListAdapter 而无需使用 xml 文件。

在这里,我认为您正在使用 Activity 进行扩展,然后无需使用 setListAdapter ..

于 2012-04-11T04:52:11.350 回答
0

只需替换您的 ListView 代码。

<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>

这将使您的应用程序完全运行。

于 2012-04-11T04:31:57.023 回答
0

在下面,检查您传递的参数,

ListAdapter adapter = new SimpleAdapter(this,
  mylist, 
  R.layout.grouplistlayout, 
  new String[] { "Group Name" }, 
  new int[] { R.id.listview_grouplist });
  1. myList地图?
  2. 你发送的资源参数应该是提供ure item而不是ure list布局的资源。您正在传递 ure list 的资源(假设 grouplistlayout 是您在上面提供的布局)。改变它。
  3. 同样,to应该包含映射到from参数的资源中提到的文本视图。

查看官方 SimpleAdapter 文档以获取更多详细信息

于 2012-04-11T04:47:44.690 回答