Today, in my Android App Developer Course by Rob Percival, he showed us how to make lists in our apps. As is his style, he shows us the meat and potatoes of something, and then challenges us to garnish it.

During the challenge, we were supposed to make the list clickable, and when clicked, have it pop-up with a toast to say what item in the list you clicked on. This is mostly just a note for my own reference, but after completing the challenge, Rob showed us a much cleaner way to do that. His method reduced 2 of my lines down to one. Here’s what I did:

MainActivity.java

[CODE]

package com.alaskalinuxuser.testlistviews;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.lang.String;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Defining the list view that I want by id.
        final ListView theList = (ListView) findViewById(R.id.theList);

        // Defining an array of names.
        final List myList = new ArrayList();

        // Adding names to the array list.
        myList.add("Dad");
        myList.add("Mom");
        myList.add("Brother");
        myList.add("Sister");
        myList.add("Uncle");
        myList.add("Aunt");
        myList.add("Grandma");
        myList.add("Grandpa");
        myList.add("First Cousin");
        myList.add("Second Cousin");
        myList.add("Double Cousin");
        myList.add("Friend");

        // Defining an adapter, to adapt my array list to the correct format.
        ArrayAdapter<String> addaptedAray = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);

        // Using the adapter to adapt my array list to the defined list view that I declared already.
        theList.setAdapter(addaptedAray);

        // Setting up a listener to "listen" for me to click on something in the list.
        theList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            // Overriding the generic code that Android uses for list views to do something specific.
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int z, long l) {

                /*
                 * My method, that I came up with during the class....
                 */
                // Converting the integer of "z" to a string with the name of the item clicked in the list.
                String a = (String) myList.get(z);

                // Logging that I tapped said item in the list.
                Log.i("tapped", a);

                // A simple toast to inform the user of what they clicked on in the list.
                Toast.makeText(getApplicationContext(), "Hi " + a , Toast.LENGTH_LONG).show();

                /*
                 * The method that Rob showed us in the class.... Much simpler code, very straight forward.
                 * public void onItemClick(AdapterView<?> adapterView, View view, int z, long l) {
                 *
                 * Toast.makeText(getApplicationContext(), "Hello " + myList.get(z) , Toast.LENGTH_LONG).show();
                 *
                 * }
                 */

            }
        });


    }
}

[/CODE]

activity_main.xml

[CODE]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.alaskalinuxuser.testlistviews.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/theList" />
</RelativeLayout>

[/CODE]

Herein lies the key part, though, where I differed from the instructor….

my way:

[CODE]

public void onItemClick(AdapterView<?> adapterView, View view, int z, long l) {

    /*
     * My method, that I came up with during the class....
     */
    // Converting the integer of "z" to a string with the name of the item clicked in the list.
    String a = (String) myList.get(z);

    // Logging that I tapped said item in the list.
    Log.i("tapped", a);

    // A simple toast to inform the user of what they clicked on in the list.
    Toast.makeText(getApplicationContext(), "Hi " + a , Toast.LENGTH_LONG).show();

[/CODE]

The better, cleaner way, as shown by Rob:

[CODE]

public void onItemClick(AdapterView adapterView, View view, int z, long l) {

 Toast.makeText(getApplicationContext(), "Hello " + myList.get(z) , Toast.LENGTH_LONG).show();

 }
[/CODE]

Much simpler. I often forget that we don't have to define something beforehand, then use it. We usually can define it at the moment of use, like Rob did here. You can download the app and check it out if you would like:

http://www.mediafire.com/file/o1cpndmwq3dl2cm/tablesandlists.apk

I also highly recommend the course, it is great! You can find it on https://www.udemy.com.

Linux - keep it simple.

Leave a Reply

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