KIO
Kreative Ideen online
ListView

ListView

The most basic way to connect items to a ListView (static data)

res/values/strings.xml

<resources>
    <string-array name="options">
        <item>Drinks</item>
        <item>Food</item>
        <item>Stores</item>
    </string-array>
</resources>

The ListView in layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TopLevelActivity">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_options"
        android:entries="@array/options"></ListView>
</LinearLayout>

For nonstatic data, use an adapter

If you need to display data in a list view that comes froma source other than strings.xml(such as a Database), you need to use an adapter. An adapter acts as a bridge between the data source and the list view.

There are several different types of adapter. For now, we’re going to focus on array adapters.

You use an array dapter by initializing it and then attaching it to the list view. To initialize the array adapter, you first specify that type of data is contained in the array you want to bind to the list view. You then pass the array adapter three paramteters:

  • a Context(usually the current activity
  • a layout resource the specifies how to display each item
  • the array itself

Here’s the code to create an array adapter that display drinks data from a array
Click here to see cursor adapter for databaseconnection

ArrayAdapter<Drink> listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list,Drink.drinks);

You then attach the array adapter to the list view using the ListView setAdapter() method.

ListView listDrinks = (ListView) findViewById(R.id.list_drinks);
listDrinks.setAdapter(listAdapter);

Full code for context…

package eu.kio.starbuzz;

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

public class DrinkCategoryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drink_category);
        ArrayAdapter<Drink> listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,Drink.drinks);

        ListView listDrinks = (ListView)findViewById(R.id.list_drinks);
        listDrinks.setAdapter(listAdapter);
    }
}

Get list views to respond to clicks

Event listener

You make the items in a list view respond to clicks by implementing an eventlistener. And event listener allows you to listen for events that take place in youre app.

AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
            /*
            * The view that was clicked (in this case, the list view)
            * This parameters give you info about which item was clicked in the list view, 
            * such as the itemss view and its position*/
            public void onItemClick(AdapterView<?> listView,View itemView,int position,long id){
                if(position == 0){
                    Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class);
                            startActivity(intent);
                }
            }
        }

Once you’ve created the listener, you need to add it to the list view

        //Add the listener to the list view
        ListView listView = (ListView)findViewById(R.id.list_options);
        listView.setOnItemClickListener(itemClickListener);

Heres the full code for context …

package eu.kio.starbuzz;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.ListView;
import android.view.View;

public class TopLevelActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_level);
        /*Create an OnItemClickListener
        * OnItemClickListener is a nested class within the AdapterView class.
        * A ListView is a subclass of AdapterView.
        * */
        AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
            /*
            * The view that was clicked (in this case, the list view)
            * This parameters give you info about which item was clicked in the list view,
            * such as the itemss view and its position*/
            public void onItemClick(AdapterView<?> listView,View itemView,int position,long id){
                if(position == 0){
                    Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class);
                            startActivity(intent);
                }
            }
        } ;

        //Add the listener to the list view
        ListView listView = (ListView)findViewById(R.id.list_options);
        listView.setOnItemClickListener(itemClickListener);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *