How to set a simple adapter to listview?

落花浮王杯 提交于 2019-12-03 14:20:25
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_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.

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

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

Just Replace your Code of 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.

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 myList a 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 to should contain the textviews mentioned in resource mapped to from parameter.

Check the offical SimpleAdapter documentation for more details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!