Question

i've just started playing around with android and i followed a layout tutorial about customizing a ListView layout on this website.

Since all the list are populated using arrays and all share the same id -->(R.id.ListViewId), i was wondering that how do i implement an onItemClick listener on each individual color? I'm trying to create an activity for each color, like for example if i click on Red, it will start a new activity, and if i click on Blue, it will start a different activity etc.

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
    <ListView
       android:id="@+id/ListViewId"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

Java

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

public class ColorList extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String[] items = {"red", "blue","green"};

        ListView listView = (ListView) findViewById(R.id.ListViewId);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
    }
}

This is what i've tried....but i can't figure out how to do it...any ideas? Please.

        ListView listView = (ListView) findViewById(R.id.ListViewId);
        listView.setAdapter(new UserItemAdapter(this, android.R.layout.simple_list_item_1, users));
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> a, View v, int pos, long id){
              // some code here to start a new activity
              // the code below is just for testing, on click it will show a NullPointerException error
              Object o = null;
              o.toString();
          }
      });
Was it helpful?

Solution

Your onItemClick is provided both the view itself and the position within the list; you need to use one of these to determine the information you want. In your simply example, position will be 0, 1, or 2 for "red", "blue", and "green". In a more complicated example, you can override creation of the view within your code and you can use setTag() to attach details you may need later (if position is not sufficient).

OTHER TIPS

You have Object o = null; o.toString();. That is giving you your Null Pointer. Get rid of that and you'll be able to put in some real code.

Check out this tutorial on how to work with the click listener.

Try listening to to onListItemClick, instead of onItemClick.

In onItemClick() use the View and get the text...

String colourString = ((TextView) v).getText();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top