Android Lesson 7: work with ListView


To learn about ListView, we create some simple examples for ListView.

Example 1: Simple ListView

Step 1: create new project
Step 2: File content_main.xml

Add a ListView:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TUTORIALSPOTS.COM"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#FF0000"
        android:textSize="30dp"
        android:id="@+id/textView" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp" />

android ListView example 1

Step 3: file MainActivity.java

package com.tutorialspots.listview;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        listView = (ListView) findViewById(R.id.listView);
        
        // List values
        String[] values = new String[]{
            "Simple ListView",
            "Android ListView",
            "ListView Adapter",
            "ListView example",
            "Android ListView tutorial"
        };

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                values
        );

        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String value = (String) listView.getItemAtPosition(i);
                Toast.makeText(getApplicationContext(), "Value selected: " + value, Toast.LENGTH_LONG).show();
            }
        });
    }
...
}

Explanation:
android.R.layout.simple_list_item_1: an Android internal layout view
android.R.id.text1 : In android.R.layout.simple_list_item_1 already defined text fields:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

Step 4: Run

android ListView result

Example 2: use android.R.layout.simple_list_item_single_choice

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1,
                values
        );
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int count = listView.getCheckedItemCount();
                if(count>0){
                    int pos = listView.getCheckedItemPosition();
                    String value = (String) listView.getItemAtPosition(pos);
                    Toast.makeText(getApplicationContext(), "Value selected: " + value, Toast.LENGTH_LONG).show();
                }
            }
        });

ListView simple list item single choice

Example 3: use android.R.layout.simple_list_item_checked

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_checked,
                android.R.id.text1,
                values
        );
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int count = listView.getCheckedItemCount();
                if(count>0){
                    int pos = listView.getCheckedItemPosition();
                    String value = (String) listView.getItemAtPosition(pos);
                    Toast.makeText(getApplicationContext(), "Value selected: " + value, Toast.LENGTH_LONG).show();
                }
            }
        });

ListView simple list item checked

Example 4: use android.R.layout.simple_list_item_multiple_choice

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice,
                android.R.id.text1,
                values
        );

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String selected = "";
                String value = "";
                int count = listView.getCheckedItemCount();
                if(count>0){
                    SparseBooleanArray pos = listView.getCheckedItemPositions();
                    for(int j=0;j<listView.getCount();j++){
                        if(pos.get(j)){
                            value = (String) listView.getItemAtPosition(j);
                            selected += value + "\n";
                        }
                    }
                    Toast.makeText(getApplicationContext(), "Value selected: " + selected, Toast.LENGTH_LONG).show();
                }
            }
        });

ListView simple_list_item_multiple_choice

1 Comment

Leave a Reply