简体   繁体   中英

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>

在这种情况下,您的Button处于焦点状态,所以listiten setOnItemClickListener不起作用..因此使它们可聚焦为false ..在Button的xml中添加此行

android:focusable="false"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM