如何设置一个简单的适配器的ListView? [英] How to set a simple adapter to listview?

查看:131
本文介绍了如何设置一个简单的适配器的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,将数组列表到列表视图,将解释一下我的问题在这里..告诉我什么是错在这里...我有三个线性布局,并在中间布局我有列表视图,如图所示的XML下面的文件..     `      

 <的LinearLayout
    机器人:ID =@ + ID / linearLayout1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>

    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=按钮/>

    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_weight =0.59
        机器人:SRC =@可绘制/ x_title/>

    <按钮
        机器人:ID =@ + ID /按钮3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=按钮/>
< / LinearLayout中>

<的LinearLayout
    机器人:ID =@ + ID / linearLayout_grouplist
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =0.09
    机器人:方向=垂直
    机器人:weightSum =1>

    <的ListView
        机器人:ID =@ + ID / listview_grouplist
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1>
    < / ListView控件>

< / LinearLayout中>

<的LinearLayout
    机器人:ID =@ + ID / linearLayout2
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>

    <按钮
        机器人:ID =@ + ID /将Button4
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=按钮/>

    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0.39
        机器人:文本=TextView的/>

    <按钮
        机器人:ID =@ + ID / button5
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=按钮/>
  < / LinearLayout中>
< / LinearLayout中>`
 

当我尝试将项目添加到列表中与阵列适配器,它的做工精细的me..but它不工作的清单适配器。它崩溃。

  ListAdapter适配器=新SimpleAdapter(这一点,
      mylist中,
      R.layout.grouplistlayout,
      新的String [] {组名},
      新的INT [] {R.id.listview_grouplist});

的String []值=新的String [] {机器人,iPhone,WindowsMo​​bile的,
      黑莓,WebOS的,Ubuntu的,Windows7的,最大OS X,
      Linux的,OS / 2};

ArrayAdapter<字符串>适配器1 =新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,价值观);
lst_projlist.setAdapter(适配器1);
 

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

解决方案

您的问题是在这里:

  R.layout.grouplistlayout,
    新的INT [] {R.id.listview_grouplist});
 

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

另外,我没有看到你在哪里适配器绑定到ListView。

SimpleAdapter文档

 公共SimpleAdapter(上下文的背景下,
                     名单,LT ;?扩展地图<字符串,>>数据,
                     INT资源,
                     的String []而成,
                     INT []键)
 

  

自:API级别1

     

构造

     

参数
  背景 其中观与此SimpleAdapter相关的上下文运行
  数据 地图列表。在列表中的每个条目对应于所述列表中的一个行。所述地图包含每个行的数据,并应包括所有的从中所指定的条目
  资源 的视图布局定义视图这个列表项目资源标识符。布局文件应包括到
定义的至少是那些命名视图    将被添加到每个项目相关的地图列名的列表。
   ,应该在从参数显示列的意见。这些都应该是TextViews。在此列表中的前N个观点是从参数给出了第一个N列的值。

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.. `

 <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 ?

解决方案

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_1 The bottom one should be pointing to to a TextView, not a list view.

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

From the 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.

这篇关于如何设置一个简单的适配器的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆