错误 - 如何将setOnItemClickListener设置为listView?

时间:2015-01-29 12:21:58

标签: android listview android-studio

我正在尝试将方法setOnItemClickListener设置为listview,但它报告错误

enter image description here

//*******///

public class ViewAdapter extends BaseAdapter
    {

        LayoutInflater mInflater;

        public ViewAdapter ()
        {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount ()
        {
            return favoriteList.size();
        }

        @Override
        public Object getItem (int position)
        {
            return null;
        }

        @Override
        public long getItemId (int position)
        {
            return position;
        }

        @Override
        public View getView (final int position, View convertView, ViewGroup parent)
        {
            if (convertView == null)
            {
                convertView = mInflater.inflate(R.layout.itemlista, null);
            }

            final TextView infraText = (TextView) convertView.findViewById(R.id.tv_infra);
            infraText.setText(favoriteList.get(position).getInfraccion());

            final TextView ageText = (TextView) convertView.findViewById(R.id.matricula);
            ageText.setText(favoriteList.get(position).getMatricula());

            final TextView fechaText = (TextView) convertView.findViewById(R.id.tv_dia);
            fechaText.setText(favoriteList.get(position).getFecha());

            final TextView horaText = (TextView) convertView.findViewById(R.id.tv_hora);
            horaText.setText(favoriteList.get(position).getHora());

            final TextView tipoinfraText = (TextView) convertView.findViewById(R.id.tv_tipo);
            tipoinfraText.setText(favoriteList.get(position).getTipoinfra());

            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0,
                                        View arg1, int position, long arg3)
                {
                    Toast.makeText(InciEnviadasActivity.this, "posicion:" + position, Toast.LENGTH_LONG).show();
                }
            });

            return convertView;
        }

    }

创建//

//******//
//list = (Button) findViewById(R.id.list);
        list =(ImageView) findViewById(R.id.list);
        list.setOnClickListener(listOnClick);
        listView = (ListView) findViewById(R.id.listView);
        db = new BBDD(this, "BBDD", null, 1);
        ;

        View.OnClickListener listOnClick = new OnClickListener()
        {
            @Override
            public void onClick (View v)
            {
                favoriteList = db.getFavList();
                listView.setAdapter(new ViewAdapter());

            }
        };

        //list = (Button) findViewById(R.id.list);
        list.setOnClickListener(listOnClick);
        listView = (ListView) findViewById(R.id.listView);
        db = new BBDD(this, "BBDD", null, 1);
        ;


//******/

我能做错什么?有什么建议可以解决吗?

我顺便从内部SQlite数据库填充ListView。

更新:已解决,我对名称LISTVIEW感到困惑,我正在尝试将方法分配给IMAGEVIEW,我很抱歉

2 个答案:

答案 0 :(得分:4)

您尝试将setOnItemClickListener改为ImageView

应为listView.setOnItemClickListener...

答案 1 :(得分:2)

listImageViewsetOnItemClickListener没有ImageView

在适配器的getView中设置新适配器也不正确,只在Activity中设置一次。与在onClick方法中设置新适配器相同,这不是一个好主意。