How to set a simple adapter to listview?
Asked Answered
W

4

6

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 ?

Warsle answered 11/4, 2012 at 4:22 Comment(1)
show the Logcat output for the CrashingChang
I
5

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.

Insecurity answered 11/4, 2012 at 4:50 Comment(2)
Thanks, I changed "R.layout.grouplistlayout" to "android.R.layout.simple_list_item_1 " .. now getting empty list, but for the second one .. should I create one TextView inside the list view ?Warsle
You should look inside the layout you specified and use whatever is in there. I think you'll find it's android.R.id.ListItem1Insecurity
A
1

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

Alton answered 11/4, 2012 at 4:52 Comment(0)
J
0

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.

Jernigan answered 11/4, 2012 at 4:31 Comment(5)
This could help but... how do you know he is using ListActivity and not simply Activity?Cerebrum
@Cerebrum exactly...typing the same !!Chang
@Cerebrum : i think that is the maximum possibility, cas from the code everything seems Perfect.Jernigan
Yeah, there is a great chance for that to happen (it has happen to me a lot of times). However, the code is actually pretty ugly... for instance, what's the SimpleAdapter for? I don't see why/where he is using that?Cerebrum
@Cerebrum I am not using ListActivity, I need to pass list of map items to the list .. so I am using SimpleAdapter, when I add lst_projlist.setAdapter(adapter1); It was crashed but after changing layout to "android.R.layout.simple_list_item_1" its not crashingWarsle
R
0

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

Rill answered 11/4, 2012 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.