如何设置一个简单的适配器到listview?

Pra*_*ady 6 android android-listview simpleadapter

我有问题添加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>`
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这里缺少什么?

Bar*_*rak 5

你的问题在这里:

    R.layout.grouplistlayout,
    new int[] { R.id.listview_grouplist }); 
Run Code Online (Sandbox Code Playgroud)

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

另外,我没有看到您将适配器绑定到listview的位置.

来自SimpleAdapter文档:

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

自:API级别1

构造函数

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