Android:ListView

From Juneday education
Jump to: navigation, search

Introduction

This chapter will introduce you to a structured way for presenting many items in a scrollable list.

Videos

Note: There's a missing method call in this video. You need to add the following to your Activity: registerForContextMenu(listView); in order to tell Android that you want to use ContextMenu.


All videos in this channel: ListView


Description

In short you only need to do the following to use a ListView with a ContextMenu. The complete source to steps below can be found at github: listviewexample.

Note: There are other ways to create na use a ListView.

Create a ListView

Add the following to your Activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="se.juneday.examples.listviewexample.MainActivity">

   <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/member_list">
   </ListView>

</LinearLayout>

Create Adapater

You need to create an Adapter and use that adapter in the ListView.In your Activity, typically in the onCreate:

        // Lookup ListView
        ListView listView = (ListView) findViewById(R.id.member_list);

        // Create Adapter
        adapter = new ArrayAdapter<Member>(this,
                android.R.layout.simple_list_item_1,
                members);

        // Set listView's adapter to the new adapter
        listView.setAdapter(adapter);


Handle ListView clicks

When a user clicks on an item the following method is invoked: onItemClick. Add an implementation of that method to your Activity:

        listView.setOnItemClickListener(new ListView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent,
                                    final View view,
                                    int position /*The position of the view in the adapter.*/,
                                    long id /* The row id of the item that was clicked */) {
                Log.d(LOG_TAG, "item clicked, pos:" + position + " id: " + id);
            }
        });


Register for ContextMenu

In your Activity, typically in the onCreate:

        registerForContextMenu(listView);

Create ContextMenu

When a user clicks (long click) on an item the following method is invoked: onCreateContextMenu. Add an implementation of that method to your Activity:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        ListView listView = (ListView) v;
        AdapterView.AdapterContextMenuInfo acMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
        Member member = (Member) listView.getItemAtPosition(acMenuInfo.position);
        menu.setHeaderTitle("Manager " + member);
        menu.add(Menu.NONE, MENU_ENTRY_DELETE, Menu.NONE, "Delete");
        menu.add(Menu.NONE, MENU_ENTRY_DUPLICATE, Menu.NONE, "Duplicate");
    }

Handle ContextMenu click

When a user clicks on a menu item (in the ContextMenu) the following method is invoked: onContextItemSelected. Add an implementation of that method to your Activity:

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // get extra information set by the View that added this menu item
        AdapterView.AdapterContextMenuInfo info =
                (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        Log.d(LOG_TAG, " itemId: " + item.getItemId());
        Log.d(LOG_TAG, " title:  " + item.getTitle());
        if (item.getItemId() == MENU_ENTRY_DUPLICATE) {
            Log.d(LOG_TAG, " member: " + members.get(info.position));
            members.add(members.get(info.position));
            ((ListView)findViewById(R.id.member_list)).invalidateViews();
        }
        return true;
    }

External videos

Links

Exercises for this chapter

There are no exercises in this chapter. Instead we have a couple of exercises on ListView in the coming chapter Android:Exercsises_Your_own_classes.

Files

  • No files for this chapter

External links (further reading)

http://www.vogella.com/tutorials/AndroidListView/article.html

Books this chapter is a part of

Android - the practical way

Book TOC | previous chapter | next chapter