android - 监听器不适用于 ListView

标签 android android-layout android-listview

<分区>

我的 ListView 和监听器工作正常,但是当我在 row.xml< 中的一个 textview 中添加 android:autoLink:"web" 属性时/strong> ,列表监听器不工作。为什么会这样? 这是 row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp" 
   >

    <TextView
        android:id="@+id/ID"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text="First" />

    <TextView
        android:id="@+id/Name"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="56dp"
        android:text="Second" />

    <TextView

          android:autoLink="web"
        android:id="@+id/Link"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/ID"
        android:layout_marginTop="18dp"
        android:text="Third" />

</RelativeLayout>

这里是listview.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Favourtes"
        android:textSize="25sp"
        android:textStyle="bold" >
    </TextView>

    <ListView
        android:id="@+id/listforplace"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical" >
    </ListView>

</LinearLayout>

这里是 .java 类:

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.bookmark);
        // loadSharedPreferencesforplacelist();
        list = (ListView) findViewById(R.id.listforplace);
        myAdapter = new DbManager(this);
        cursor = myAdapter.getAllfav();
        // updateList();
        String[] from = new String[] {/* DbManager.PlaceID, */DbManager.ID,
                DbManager.name, DbManager.referance };
        int[] to = new int[] { R.id.ID, R.id.Name, R.id.Link };
        cursorAdapter = new SimpleCursorAdapter(this, R.layout.bookmark_row,
                cursor, from, to);

        list.setAdapter(cursorAdapter);
        // cursorAdapter.setViewBinder(new ScoreHistoryBinder());
        list.setOnItemClickListener(listOnItemClickListener);

    }

    public ListView.OnItemClickListener listOnItemClickListener = new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Cursor cursor = (Cursor) parent.getItemAtPosition(position);
            /* final int */placeidfordelete = cursor.getInt(cursor
                    .getColumnIndex(DbManager.ID));
            // int placename =
            // cursor.getString(cursor.getColumnIndex(DbManager.ID));
            placelongi = cursor
                    .getString(cursor.getColumnIndex(DbManager.name));
            placelati = cursor.getString(cursor
                    .getColumnIndex(DbManager.referance));
            placealti = cursor.getString(cursor.getColumnIndex(DbManager.des));

            AlertDialog.Builder builder = new AlertDialog.Builder(viewmark.this);
            builder.setTitle("Delete");
            builder.setIcon(android.R.drawable.ic_dialog_info);

            builder.setMessage("Delete marks");
            builder.setPositiveButton("Delete",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {

                            myAdapter.delete_byID_place(placeidfordelete);
                            updateList();

                        }
                    });
            builder.setNegativeButton("All delete",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                        }

                    });

            AlertDialog diag = builder.create();
            diag.show();

        }
    };

    @Override
    protected void onDestroy() { // TODO Auto-generated method stub
        super.onDestroy();
        cursor.close();
        myAdapter.close();

    }

    @SuppressWarnings({ "deprecation" })
    private void updateList() {
        cursor.requery();
        cursor.close();
        // //emptyFormFields();
    }

}

最佳答案

这是因为 autoLink web 属性导致 TextView 元素窃取点击事件并阻止它们冒泡到 ListView。

关于android - 监听器不适用于 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15265316/

相关文章:

android - 添加的 View 将 RecyclerView 作为父 View ,但 View 不是真正的 subview

Android - 增值监听器导致应用程序崩溃

安卓 : Getting User input and Display in RecyclerView

android - WorkManager 的 OneTimeWorkRequest 的重试策略/机制是什么

java - Android - 错误膨胀类 fragment - XML 文件

java - 我正在制作一个双 Webview,它有问题请帮助我

Android BottomNavigationView 截断文本

android - 我如何在 Android API 级别 10 上实现带有 ListView 的 SlidingDrawer?

android - 在 SQLite 和 ListView 之间格式化数据

android - 与应用程序链接主机的 facebook 深层链接