【问题标题】:How to set a simple adapter to listview?如何为listview设置一个简单的适配器?
【发布时间】:2012-04-11 04:22:47
【问题描述】:

我在将 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);

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

【问题讨论】:

  • 显示崩溃的 Logcat 输出

标签: android android-listview simpleadapter


【解决方案1】:

你的问题在这里:

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

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

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

来自SimpleAdapter docs

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

自:API 级别 1

构造函数

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

【讨论】:

  • 谢谢,我将“R.layout.grouplistlayout”更改为“android.R.layout.simple_list_item_1” .. 现在得到空列表,但是对于第二个 .. 我应该在里面创建一个 TextView列表视图?
  • 您应该查看您指定的布局并使用其中的任何内容。我想你会发现它是 android.R.id.ListItem1
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    只需替换您的 ListView 代码。

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

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

    【讨论】:

    • 这可能会有所帮助,但是...您怎么知道他使用的是ListActivity 而不仅仅是Activity
    • @Cristian 完全一样...打字一样!!
    • @Cristian :我认为这是最大的可能性,从代码来看,一切看起来都很完美。
    • 是的,这很有可能发生(我已经发生过很多次了)。但是,代码实际上非常难看......例如,SimpleAdapter 是什么?我不明白他为什么/在哪里使用它?
    • @Cristian 我没有使用 ListActivity,我需要将地图项列表传递给列表 .. 所以我使用 SimpleAdapter,当我添加 lst_projlist.setAdapter(adapter1);它崩溃了,但是在将布局更改为“android.R.layout.simple_list_item_1”后它没有崩溃
    【解决方案4】:

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

    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 参数的资源中提到的文本视图。

    查看offical SimpleAdapter documentation了解更多详情

    【讨论】:

      猜你喜欢
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2015-09-29
      • 2017-11-05
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      相关资源
      最近更新 更多