Android: When I set an OnClickListener for a ListView

后端 未结 4 2045
鱼传尺愫
鱼传尺愫 2021-01-12 23:34
    lv.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

        }
    });

Does v refer to the ListView

相关标签:
4条回答
  • 2021-01-12 23:49

    There is a setOnItemClickListener for what you want.

    0 讨论(0)
  • 2021-01-12 23:50

    onClickListener just listens to a click on the entire View. v will be the ListView.

    What you probably want is onItemClickListener instead of onClickListener.

    Or you may want to extend ListActivity and override onListItemClick()

    0 讨论(0)
  • 2021-01-13 00:01

    I believe that v refers to the whole ListView. To add a click handler for an item, you want to use AdapterView.setOnItemClickListener.

    0 讨论(0)
  • 2021-01-13 00:14
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,long itemID) {
    
            View itemView = view;
            String fromDate = ((TextView)itemView.findViewById(R.id.fromDate)).getText().toString();
            int position = (int) arg0.getSelectedItemId();
            Log.d(TAG, "ListView Selected : "+fromDate+" Position:"+position+" arg2:"+arg2+" arg3:"+itemID);
        }
    });
    
    0 讨论(0)
提交回复
热议问题