Android CustomListView OnClick

时间:2017-07-23 06:58:29

标签: java android

Hello Everyone Iam正在创建一个AndroidStudio List视图应用程序,我注意到自定义列表视图要好得多,所以我决定切换到自定义!

我的CustomListView的onclick监听器有问题我无法获得点击项目的位置,所以我无法从中收集信息

  

因为:

这是我的Listview OnClick ListenerCode(来自互联网)

switch

它有效并且在OnItemClickListener()上出现错误! 说:

 list.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position,
                                long arg3)
        {
            // here the position element of this method will give you the row number on which the click happened.
            //Then you can get individual values like name[position], address[position] and then pass these values through an intent
        }
    });

我使用ALT + ENTER来修复它创建的错误

 Error:(118, 37) error: incompatible types: <anonymous android.support.v7.internal.widget.AdapterViewCompat.OnItemClickListener> cannot be converted to android.widget.AdapterView.OnItemClickListener
Error:(120, 37) error: cannot find symbol class AdapterView
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

在OnItemListClickListener()和Suggest后面实现方法,所以我做ALT + ENTER,它会像:

AdapterViewCompat.

它在第一个OnItemClick功能上给出错误我虽然它是第二个的重复,所以我删除了但是当我这样做时它也会在第二个上给出错误! 说:

 list.setOnItemClickListener(new AdapterViewCompat.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position,
                                long arg3)
        {
            // here the position element of this method will give you the row number on which the click happened.
            //Then you can get individual values like name[position], address[position] and then pass these values through an intent
        }

        @Override
        public void onItemClick(AdapterViewCompat<?> adapterViewCompat, View view, int i, long l) {

        }
    });

来自互联网剂量的代码

  

问题:OnClickListener对于CustomListView可能吗?

1 个答案:

答案 0 :(得分:2)

ListView的setOnItemClickListener侦听器将android.widget.AdapterView.OnItemClickListener作为参数,您尝试将此android.support.v7.internal.widget.AdapterViewCompat.OnItemClickListener设置为错误。

将其更改为正确的侦听器类型,并且应该解决错误。

list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

                     //implement methods
})