android - ListView 中的 onItemClick 不起作用

标签 android android-listview onitemclicklistener

我无法收到 Toast 消息,因为从未调用过 onItemClick。 Log-cat 没有显示任何错误。如果我在任何地方出错,请检查我的代码并纠正我。我用过阵列适配器。

public class Open extends ListActivity {
    DbAdapter mDb = new DbAdapter(this);

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

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open);

        ListView listContent = (ListView) findViewById(android.R.id.list);

        mDb.open();
        Cursor cursor =mDb.fetchAllPrograms();
        startManagingCursor(cursor);

        String[] from = new String[] { DbAdapter.KEY_TITLE };
        int[] to = new int[] { R.id.textViewName };

        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
    R.layout.row, cursor, from, to);
        listContent.setAdapter(cursorAdapter);

        //Onclick ListView setlistener
        listContent.setTextFilterEnabled(true);
        listContent=getListView();


            listContent.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View 

    view,int position, long id) {
                    Toast.makeText(getApplicationContext(),
   "Working!", 
                    Toast.LENGTH_LONG).show();    
                }
            });

    }
}

Row.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/textViewName"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginTop="10dp"
     android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#0000FF"
    android:textIsSelectable="true"
     />

最佳答案

我遇到了类似的问题。不仅我的 onItemClick 方法被忽略了,而且我的列表选择器也没有工作(android:listSelector。)

结果是我的 TextView 行元素上的 android:textIsSelectable 属性。尝试将其设置为 false

关于android - ListView 中的 onItemClick 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15216178/

相关文章:

java - 两种实现回调的方式,那么他有什么区别

php - android使用php脚本从数据库中检索值

android - 自定义 RecyclerClickListener 无法针对多个 onClicks 正常工作

android - 单击列表中某个位置的一个 View 和不同位置自动工作的其他 View ?

android - Android单元测试中如何通过ID访问资源?

带类别的 Android GridView?

android - 在 ListView 中更改文本颜色

android - AdapterView.OnItemClickListener() 在我的 customAdapter 中不起作用

Android Gridview 和按钮 OnItemclick

android - 如何使用 Retrofit Android 实现 OAuth 2.0