Marcus Thornton
Marcus Thornton

Reputation: 6193

Why my Android setOnItemClickListener doesn't work?

I have problem in setting setOnItemClickListener. The following is my code. I've tested that setAdapter worked and the list and items were shown on the UI. When it came to setting setOnItemClickListener, it didn't work.

cool_simpleAdapter = new SimpleAdapter(this, items,
    R.layout.mylistitem, new String[] { "title", "link" }, new int[] {
            R.id.textView_title, R.id.textView_link });
cool_listView.setAdapter(cool_simpleAdapter);
Log.d("tag_1", "before setOnItemClickListener");
cool_listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Log.d("tag_setonItemClick", "in onItemClick");
        Uri uri = Uri.parse("http://www.google.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
});
Log.d("tag_2", "after setOnItemClickListener");

I put the log to trace what happened:

Log.d("tag_1","before setOnItemClickListener");

and

Log.d("tag_2","after setOnItemClickListener");

were displayed but

Log.d("tag_setonItemClick","in onItemClick");

were not displayed. And I cannot click on the item, neither open the URL. I don't know how should I fix the problem.

Edit: add mylistitem.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView_link"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Upvotes: 1

Views: 5459

Answers (2)

navneet sharma
navneet sharma

Reputation: 680

in this condition your Button is focused so listiten setOnItemClickListener not working..so make them focusable false.. add this line in xml of Button on

android:focusable="false"

Upvotes: 14

Patato
Patato

Reputation: 1472

I had this problem before, that was because each item of listview had a Button. to solve this just need to set android:focusable of the button to false. I hope this will help you

Upvotes: 2

Related Questions