Lesson 42. List - ListView

Lesson 42. List – ListView


In this lesson:

– We use ListView to build the list

Before you start talking about the component ListView, I would like to mention again the past lesson and the mechanism for building the list we used there. We sorted through an array of data, creating in each iteration item list, filled it out data and put in list.

When creating ListView will create items for us adapter. We need the adapter from us data and layout resource item list. Next, we assign adapter list ListView. The list asks the adapter for items, the adapter creates them (using data and layout) and returns the list. As a result, we see a ready list.

There are different types of lists and adapters. For now, we will look at the simplest option.

Let’s create a project:

Project name: P0421_SimpleList
Build Target: Android 2.3.3
Application name: SimpleList
Package name: ru.startandroid.develop.p0421simplelist
Create Activity: MainActivity

open main.xml and add a component to the screen ListView (Composite tab):



    
    
    
    

ListView is the component of the list.

Now we need to create adapter. open MainActivity.java and we write:

package ru.startandroid.develop.p0421simplelist;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

  String[] names = { "Иван", "Марья", "Петр", "Антон", "Даша", "Борис",
      "Костя", "Игорь", "Анна", "Денис", "Андрей" };

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // находим список
    ListView lvMain = (ListView) findViewById(R.id.lvMain);

    // создаем адаптер
    ArrayAdapter adapter = new ArrayAdapter(this,
        android.R.layout.simple_list_item_1, names);

    // присваиваем адаптер списку
    lvMain.setAdapter(adapter);

  }
}

You won’t believe it, but that’s all the code needed to create the list)

We use an array of names as data. In onCreate, we find a list, create an adapter, and assign a list adapter. Let’s see how the adapter was created.

We used this constructor: public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

and passed it the following parameters:

this – context
android.R.layout.simple_list_item_1 is a system layout file that is a TextView
names – the array of data we want to list

We can see the content used simple_list_item_1. To do this, find an item in your project Android 2.3.3., Open it, and open it android.jar

Promote to the bottom and open res.layout.

And inside we find the one we use simple_list_item_1

Double click on it and watch the content:



Plain TextView with a set of options.

When a list prompts for a next item, the adapter takes this Layout resource simple_list_item_1Chases him through LayoutInflater and receives View, Converts View to TextView, assigns to it text from the array of data and gives list.

We’ll save everything and run it. We see a list from our data.

Using the simple_list_item_1 system layout resource is good because we don’t need to draw the layout ourselves. However, if we are not comfortable with what the list looks like using simple_list_item_1 as a list item, we can create your layout resource.

Let’s create a layout file my_list_item.xml in the res / layout folder of our project:



TextView indicating the color and size of the font, the alignment of the text and the indentation.

Let’s change the existing code, specify the adapter our created layout-resource my_list_item:

    // создаем адаптер
    ArrayAdapter adapter = new ArrayAdapter(this,
        R.layout.my_list_item, 
        names);

The adapter will now use it when creating list items.

We’ll save everything and run it. We see our green list:

In the layout resource for a list item instead TextView you can use any of its derived classes – for example Button. The main thing is that the object has passed transformation to TextView. The adapter will assign text to it by method setText and give it to the list.

A bit about Context

In one of my past lessons, I said that Context (Context) is used to access basic program features. In this lesson, we have a good confirmation of these words.

ArrayAdapter uses LayoutInflater to convert the layout resource to View. But getting a LayoutInflater object is one of the basic functions and it unavailable for the class ArrayAdapter. Therefore, in ArrayAdapter we refer to Activity as a context (Activity has access to basic functions through the ascending class hierarchy). And the ArrayAdapter class inside uses handed over to him contextTo call LayoutInflater. Without context, he would not have been able to do so.

In the next lesson:

– we use ListView for single and multiple element selection

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)




Discuss in the forum [303 replies]

Leave a Comment