Android - ListView

Card Puncher Data Processing

About

ListView is a view group that shows items in a vertically scrolling list.

The items come from the ListAdapter associated with this view.

The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

Example

  • The container layout file with a list view. It seems that inside xml, the listView cannot contain child. The child must be created as an other layout file, passed to the adapter that will create and populate the child view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/dictionary_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
  • The row view for the list view. The adapter will populate it with the data.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView android:id="@+id/text1"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text1"/>

    <TextView android:id="@+id/text2"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text2"/>

    <TextView android:id="@+id/text3"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text3"/>

</LinearLayout>
  • The java code. The columns name (the projection) and the layout items match by sequence. Therefore, the two array must have the same length.
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.provider.UserDictionary.Words;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.support.v4.widget.SimpleCursorAdapter;

public class MainActivity extends ActionBarActivity {

    // For the SimpleCursorAdapter to match the UserDictionary columns to layout items.
    private static final String[] COLUMNS_NAME = new String[] {
            UserDictionary.Words._ID,
            UserDictionary.Words.WORD,
            UserDictionary.Words.FREQUENCY
    };

    private static final int[] LAYOUT_ITEMS = new int[] {
            R.id.text1,
            R.id.text2,
            R.id.text2
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the TextView which will be populated with the Dictionary ContentProvider data.
        ListView dictListView = (ListView) findViewById(R.id.dictionary_list_view);

        // Get the ContentResolver which will send a message to the ContentProvider.
        ContentResolver resolver = getContentResolver();

        // Get a Cursor containing all of the rows in the Words table.
        Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);

        // Set the Adapter to fill the standard two_line_list_item layout with data from the Cursor.
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.three_line_list_item,
                cursor,
                COLUMNS_NAME,
                LAYOUT_ITEMS,
                0); // Flags are not passed
        
        // Attach the adapter to the ListView.
        dictListView.setAdapter(adapter);
    }
}

where:

Documentation / Reference





Discover More
Androidadapteritemviewtype
Android - Adapter

An android/widget/AdapterAdapter (widget) object acts as a bridge between: an AdapterView (An android/widget/AdapterViewAdapterView is a view whose children are determined by an Adapter. See subclasses...
Android Imageview Visibility
Android - View (UI Widget|Component)

A android/view/Viewview represents a a widget. All the UI elements extends from the android/view/Viewview class a container (viewgroup) is a subclass of a view object a simple widget is also a subclass...



Share this page:
Follow us:
Task Runner