Lesson 41. Use LayoutInflater to create a list

Lesson 41. Use LayoutInflater to create a list

In this lesson:

– make your own version of the list

In the last lesson we learned why we needed a class LayoutInflater and they made a small example that looked at the method in detail inflate and its parameters. To consolidate the topic, let’s make another, slightly more complicated, example.

We will make our analogue list. Let’s start with the data. Let it be again staff schedule with employee names, positions, and salaries. That is, each item on our list will contain three non-editable text boxes – name, position, salary. And we will place the items as a vertical list.

We will need them for implementation two layout-file:
main.xml – main screen for Activity, container for list items
item.xml – a screen with FrameLayout and three text boxes in it. This will be a list item.

The application will sort through in parallel three arrays of data, create for each three View– Element from layout file item.xml, fill in its data and add in the vertical LinearLayout in main.xml.

Let’s create a project:

Project name: P0411_LayoutInflaterList
Build Target: Android 2.3.3
Application name: LayoutInflaterList
Package name: en.startandroid.develop.p0411layoutinflaterlist
Create Activity: MainActivity

screen main.xml:


ScrollView will provide us scrolling list if all the items do not fit the screen. And in it LinearLayoutTo which we will add items.

screen item.xml:


FrameLayout, and three TextView in it.

Code implementation. MainActivity.java:

package ru.startandroid.develop.p0411layoutinflaterlist;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

  String[] name = { "Иван", "Марья", "Петр", "Антон", "Даша", "Борис",
      "Костя", "Игорь" };
  String[] position = { "Программер", "Бухгалтер", "Программер",
      "Программер", "Бухгалтер", "Директор", "Программер", "Охранник" };
  int salary[] = { 13000, 10000, 13000, 13000, 10000, 15000, 13000, 8000 };

  int[] colors = new int[2];

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {

    colors[0] = Color.parseColor("#559966CC");
    colors[1] = Color.parseColor("#55336699");

    LinearLayout linLayout = (LinearLayout) findViewById(R.id.linLayout);

    LayoutInflater ltInflater = getLayoutInflater();

    for (int i = 0; i < name.length; i++) {
      Log.d("myLogs", "i = " + i);
      View item = ltInflater.inflate(R.layout.item, linLayout, false);
      TextView tvName = (TextView) item.findViewById(R.id.tvName);
      TextView tvPosition = (TextView) item.findViewById(R.id.tvPosition);
      tvPosition.setText("Должность: " + position[i]);
      TextView tvSalary = (TextView) item.findViewById(R.id.tvSalary);
      tvSalary.setText("Оклад: " + String.valueOf(salary[i]));
      item.getLayoutParams().width = LayoutParams.MATCH_PARENT;
      item.setBackgroundColor(colors[i % 2]);

Not too much code is required to make a simple list. we launch cycle by the number of elements in the data arrays. In each iteration create a View- Item item from layout file item.xml. In our case item is it FrameLayoutWhich contains three TextView. We find them in created item and fill in the data from arrays.

In the method inflate we have specified root - linLayoutTo get it LayoutParams and then used to adjust the width. Also, for clarity, color the items using the setBackgroundColor method.

Note the third parameter inflate we have specified false. That is me did not immediately add create a View element to linLayout, and we do it at the end of the code with a method addView. There is an explanation for this. If true, then the method would add an item to linLayout and return it to us linLayout, Common to all list items. through linLayout fill in TextView the text we needed would be difficult. So we get the point item (FrameLayout), fill it TextView data and only then put to other points in linLayout addView method.

Let's save and run everything:

The list has been scrolled and scrolling.

The lesson came out short but helpful. Just in case, I want to point out that this is not yet a classic Android list called List. But this example will make the list much easier to understand. Because the principle is similar. To build the List we will also need to provide an array of data and a layout file for the items. This is what we will do in the next lesson.

In the next lesson:

- We use ListView to build the list

Discuss in the forum [141 replies]

Leave a Comment