Android:没有调用Listview的onItemClick事件

问题描述

|| 以下是我的代码:
public class TestActivity extends ListActivity {

    private Cursor cursor;

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

        fillData(); 
    }

    @Override
    protected void onListItemClick(ListView l,View v,int position,long id) {
        super.onListItemClick(l,v,position,id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        Toast.makeText(this,\"You selected: \" + keyword,Toast.LENGTH_LONG)
                .show();
    }

    private void fillData() {
        TermsData_DataHelper termsDataHelper = new TermsData_DataHelper(
                TestActivity.this);
        cursor = termsDataHelper.fetchAllCategories();
        startManagingCursor(cursor);

        String[] names = new String[] { \"name\" };
        int[] to = new int[] { R.id.label };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter categories = new SimpleCursorAdapter(this,R.layout.terms_row,cursor,names,to);
        setListAdapter(categories);
    }
}
当我在列表视图中单击一行时,看不到任何Toast。谁能告诉我我在哪里犯错。 提前致谢。     

解决方法

我遇到了类似的问题,发现从列表项中删除
android:clickable=\"true\"
之后,onListItemClick()被很好地触发了。我希望这有帮助。     ,
protected void onListItemClick(ListView l,View v,int position,long id) {
    super.onListItemClick(l,v,position,id);

    String mString = parent.getItemAtPosition(position).toString();
    //will give you the text of current line or
    int i = parent.getItemAtPosition(position);
    //if you want just position number
}
    ,解决方案是重写受保护的void onListItemClick(ListView l,View v,int位置,长id)方法。     ,我也有这个问题。我通过从布局XML中的TextView中删除此问题来解决它。
    android:textIsSelectable=\"true\"
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...