How to install a simple adapter in listview?

I have a problem with adding arraylist to view the list, I will talk about my problem here .. tell me what is wrong here ... I have three linear layouts, and in the middle of the layout I have a 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 a list using an array adapter, it works fine for me ... but it doesn't work for a List Adapter. It is crumbling.

 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); 

can someone tell me what is missing here?

+6
source share
4 answers

Your problem is here:

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

The top should point to the list layout, for example android.R.layout.simple_list_item_1 bottom should point to a TextView, not a list.

Also, I don’t see where you bind the adapter to the list.

From the SimpleAdapter Docs :

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

Because: API Level 1

Constructor

Parameters
context The context in which the view associated with this SimpleAdapter data is a list of maps. Each entry in the list corresponds to one line in the list. Maps contain data for each row and must include all entries specified in "from"
resource Identifier of the view layout resource that defines the views for this list item. The layout file must include at least those named views that are defined in "on"
from . A list of column names to be added to the Map associated with each item.
Views that should display the column in the from parameter. All must be TextViews. The first N views in this list specify the values ​​of the first N columns in the from parameter.

+5
source

If you are expanding using Activity, you need to use setAdapter. And if you are expanding using ListActivity, you should use setListAdapter and you do not need to use an XML file.

And so I think that you are expanding using Activity, then you do not need to use setListAdapter ..

+1
source

Just replace your ListView code.

 <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> 

This will make your application fully executed.

0
source

Next, check the parameters you pass

 ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.grouplistlayout, new String[] { "Group Name" }, new int[] { R.id.listview_grouplist }); 
  • is there a myList card?
  • The parameter of the resource to be sent must be a resource that provides the layout of the ure element, not the list. you are passing the ure list resource (assuming grouplistlayout is the layout you specified above). change it.
  • Similarly, to must contain the text images specified in the resource associated with the from parameter.

Learn more about the SimpleAdapter official documentation.

0
source

Source: https://habr.com/ru/post/912898/


All Articles