How to add an item to Android ListView?

I made an android listView, getting help from Vogella.com using the following layout and ListActivity class.

RowLayout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/icon" android:layout_width="22px" android:layout_height="22px" android:layout_marginLeft="4px" android:layout_marginRight="10px" android:layout_marginTop="4px" android:src="@drawable/icon" > </ImageView> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:textSize="20px" > </TextView> </LinearLayout> 

MyListActivity.java

 package de.vogella.android.listactivity; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MyListActivity extends ListActivity { public void onCreate(Bundle icicle) { super.onCreate(icicle); String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; // use your own layout ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.label, values); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); } } 

I want to add subtext below the textView and keep the full text part in the center of each line. How can i do this?

+6
source share
4 answers
Element

A ListView may have its own custom layout. When you create your adapter for ListView , you can pass the layout identifier to the adapter constructor. See SimpleAdapter and ArrayAdapter .

If you want to show some additional details, such as an image and text or two text images, you will have to expand the adapter and implement the getView() property to set the image + text.

Check out Custom ListView

And if you want to classify the ListView in sections, you need to go to the ListView section in Android , and also check the Title section in the ListView

+6
source

You can use the built-in android.R.layout.simple_list_item_2 file to create two-line text.

enter image description here

+16
source

To do this, you need to create your own list view. Here is the link Link ...

0
source

To add subitems to your items in a list view, use the advanced list view.

Check out this example.

You can find out if you have additional requests :)

0
source

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


All Articles