Question

I'm asking my user for a postcode via an editText then i use a button to fetch and display store locations in a listView using a simpleCursorAdapter.

Now i want to be able to launch a new activity by selecting an item in the listView and passing along some string information.

I can't get the onClickListener to register my clicks.

Is it because i'm using an Activity instead of a ListActivity?

I'd rather have both the input (the EditText) and the results (ListView) both in the same activity.

activity_pcentry:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/enter_post" />

    <EditText
        android:id="@+id/etPostCode"
        android:imeOptions="actionSearch"
        android:inputType="textCapCharacters"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/search_button"
        android:onClick="DoPostSearch" />

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

</LinearLayout>

stockrow_group:

<?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:clickable="true"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/branch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" >
    </TextView>

    <TextView
        android:id="@+id/post"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" >
    </TextView>

    <TextView
        android:id="@+id/telephone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" >
    </TextView>


</LinearLayout>

PostCodeEntryActivity.java:

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;    

public class PostCodeEntryActivity extends Activity{

        private MyDatabase stockistsDB;
        public EditText enteredCode;
        private ListView listView;

        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pcentry);
            stockistsDB = new MyDatabase(this);
        }


        public void DoPostSearch(View v) {

            enteredCode = (EditText)findViewById(R.id.etPostCode);
            String postalCode = enteredCode.getText().toString();
            Cursor results = stockistsDB.getStockistsFromPostCode(postalCode);

            if (results != null && results.getCount() > 0) {
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                        R.layout.stockrow_group,
                        results, new String[]{"fld_BranchName","fld_PostCode","fld_Tel"},
                        new int[]{R.id.branch,R.id.post,R.id.telephone},
                        0);

                listView = (ListView)findViewById(R.id.list);
                listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
                {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        Toast.makeText(PostCodeEntryActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
                    }
                });
                listView.setAdapter(adapter);


            } else {
                Toast.makeText(PostCodeEntryActivity.this, "No results found, please check your postcode", Toast.LENGTH_SHORT).show();
            }
        }
    }
Was it helpful?

Solution

Do you have any clickable elements on the rows themselves? Please post the XML from R.layout.stockrow_group.

If you have buttons on that layout for example you might need to add the following to those buttons:

android:focusable="false"
android:focusableInTouchMode="false"

This will make sure the click on the list item is not getting blocked by the clickable item on the row itself.

The actual solution:

Do not make the rows of the listview clickable. This will "eat" up the click event by registering an empty click handler. Remove the following line from the rows of the list view:

android:clickable="true"

OTHER TIPS

  1. Implement AdapterView.onItemClickListener in your activity`
  2. Change

    listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()

    To

    listView.setOnItemClickListener(this)

  3. Add unimplemented methods

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top