Android 如何将简单的适配器设置为列表视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10099764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:28:53  来源:igfitidea点击:

How to set a simple adapter to listview?

androidandroid-listviewsimpleadapter

提问by Prady

I have problem adding arraylist to list view, will explain about my problem here.. tell me what is wrong here... I have three linear layouts, and in the middle layout i have list view, as shown in the xml file below.. `

我在将 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>`

when I try to add items to the list with array adapter, its working fine for me..but its not working for List Adapter. It is crashing.

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

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);

could someone tell me what is missing here ?

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

采纳答案by Barak

Your problem is in here:

你的问题在这里:

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

The top one should be pointing to a list layout, like android.R.layout.simple_list_item_1The bottom one should be pointing to to a TextView, not a list view.

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

Also, I don't see where you are binding the adapter to the listview.

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

From the SimpleAdapter docs:

来自SimpleAdapter 文档

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

Since: API Level 1

Constructor

Parameters
contextThe context where the View associated with this SimpleAdapter is running
dataA List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resourceResource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
fromA list of column names that will be added to the Map associated with each item.
toThe views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

从:API 级别 1

构造函数

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

回答by Hulk

If you are extending with Activity then you have to use setAdapter AND if you are extending with ListActivity then you should use setListAdapter and no need to use xml file..

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

And here i think you are extending with Activity then no need to use setListAdapter..

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

回答by Bhavin

Just Replace your Code of ListView.

只需替换您的 ListView 代码。

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

This will Make your app run Completely.

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

回答by Shubhayu

In the following, check the parameters you are passing,

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

ListAdapter adapter = new SimpleAdapter(this,
  mylist, 
  R.layout.grouplistlayout, 
  new String[] { "Group Name" }, 
  new int[] { R.id.listview_grouplist });
  1. is myLista map?
  2. The resource parameter that u r sending should be the resource which provides the layout of ure item and not of ure list. you are passing the resource of ure list (assuming grouplistlayout is the layout that u have provided above). change that.
  3. Similarly, the toshould contain the textviews mentioned in resource mapped to fromparameter.
  1. myList地图吗?
  2. 你发送的资源参数应该是提供ure item的布局而不是ure list的资源。您正在传递 ure 列表的资源(假设 grouplistlayout 是您在上面提供的布局)。改变那个。
  3. 同样,to应该包含映射到from参数的资源中提到的文本视图。

Check the offical SimpleAdapter documentationfor more details

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