无法点击为listView工作 - Android

时间:2015-01-07 07:48:33

标签: android listview android-listview android-studio onclicklistener

我无法点击listView。我已经关注了几个例子并尝试了有关历史SO页面的建议,但却没有得到它。

到目前为止我有这个代码填充我的listView。它还包含我试图用来使其可点击的代码。

helper = new TaskDBHelper(Overview.this);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();
    Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.BAL, TaskContract.Columns.IP, TaskContract.Columns.STATUS, TaskContract.Columns.CNTRY, TaskContract.Columns.VS},
            null, null, null, null, null);

    listAdapter = new SimpleCursorAdapter(
            this,
            R.layout.sum_view,
            cursor,
            new String[]{TaskContract.Columns.TASK, TaskContract.Columns.BAL, TaskContract.Columns.IP, TaskContract.Columns.STATUS, TaskContract.Columns.CNTRY, TaskContract.Columns.VS},
            new int[]{R.id.taskTextView, R.id.txtData, R.id.txtIP, R.id.txtStatus, R.id.txtCounty, R.id.imgStatus},
            0
    );


    ListView nList = this.getListView();
    nList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            Log.d("***CLICK***","YES");

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            Log.d("***CLICK***","NOTHING SELECTED");
        }
    });


    this.setListAdapter(listAdapter);

感谢您的建议。我改成了这个

    helper = new TaskDBHelper(Overview.this);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();
    Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.BAL, TaskContract.Columns.IP, TaskContract.Columns.STATUS, TaskContract.Columns.CNTRY, TaskContract.Columns.VS},
            null, null, null, null, null);

    listAdapter = new SimpleCursorAdapter(
            this,
            R.layout.sum_view,
            cursor,
            new String[]{TaskContract.Columns.TASK, TaskContract.Columns.BAL, TaskContract.Columns.IP, TaskContract.Columns.STATUS, TaskContract.Columns.CNTRY, TaskContract.Columns.VS},
            new int[]{R.id.taskTextView, R.id.txtData, R.id.txtIP, R.id.txtStatus, R.id.txtCounty, R.id.imgStatus},
            0
    );


    ListView nList = this.getListView();
    nList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent,
                                View view,int position, long id) {
            Log.d("***CLICK***","YES");
        }
    });


    this.setListAdapter(listAdapter);

但是按下列表视图项时根本没有日志文件。

另外,我的listView是由一行重复的单独布局文件组成的。

2 个答案:

答案 0 :(得分:1)

您应该使用setOnItemClickListenersetOnItemSelectedListener用于选择多个项目。

试试这个:

nList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
                            int position1, long id) {
 Log.d("***CLICK***","YES");
 }
});

答案 1 :(得分:0)

使用ListView.OnItemClickListener代替ListView.setOnItemSelectedListener在ListView行上获取点击事件:

    nList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, 
                                           View view,int position, long id) {
                // called when listview row selected
          }
     });