android - 如何将一个简单的适配器设置为 ListView ?

标签 android android-listview simpleadapter

我在将 arraylist 添加到 ListView 时遇到问题,将在这里解释我的问题.. 告诉我这里出了什么问题......我有三个线性布局,在中间布局我有 ListView ,如 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);

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

最佳答案

你的问题在这里:

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

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

此外,我没有看到您将适配器绑定(bind)到 ListView 的位置。

来自SimpleAdapter docs :

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

Since: API Level 1

Constructor

Parameters
context The context where the View associated with this SimpleAdapter is running
data A 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"
resource Resource 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"
from A list of column names that will be added to the Map associated with each item.
to The 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.

关于android - 如何将一个简单的适配器设置为 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099764/

相关文章:

android - 如何在 Android 中单击 ListView 行时使 ImageView 可见和不可见?

android - 嵌套布局 ScrollView

android - 将照片发布到 Facebook 参数

android - 如何将 ListView 扩展为 View

android - HListView 无法实例化 Android Eclipse 项目库

java - StringReader 的 NullPointerException

java - Intent 额外和 Intent 数据之间有什么区别?

Android:增加调用堆栈大小

android - ListView 和键盘导航

android - 根据项目中的 TextView 更改 ListView 行颜色