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