0

I am trying set method setOnItemClickListener to listview but It reports an error

enter image description here

code

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

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;
        }

    }

on create//

//******//
//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);
        ;


//******/

What can I be doing wrong? any suggestions to solve?

I am populate ListView from internal SQlite database by the way.

UPDATE: SOLVED, I CONFUSED THE NAME LISTVIEW, I WAS TRYING TO ASSIGN THE METHOD TO A IMAGEVIEW, I'M SO SORRY

Aspicas
  • 4,403
  • 4
  • 28
  • 51

2 Answers2

4

You trying to setOnItemClickListener to an ImageView.

Should be listView.setOnItemClickListener...

localhost
  • 5,432
  • 1
  • 30
  • 50
  • Oh God...I confused the name ... I'm an idiot, I have a good time trying to fix it, sometimes the dumbest mistakes are the worst – Aspicas Jan 29 '15 at 12:28
2

list is an ImageView, and there is no setOnItemClickListener for ImageView.

Setting a new adapter in getView of your adapter is also incorrect, set it only once in your Activity. Same for setting a new adapter in the onClick method, it is not a good idea.

2Dee
  • 8,510
  • 7
  • 40
  • 53
  • Or just tired, don't beat yourself up, everyone makes mistakes ;) Please do take in account my remarks on setting the adapter several times, it is not a good idea, it is better to update the content of the adapter every time you want to update the list. Do you need help for that ? – 2Dee Jan 29 '15 at 12:32
  • 2
    Look for "update adapter content" in Google. this answer shows the basics : http://stackoverflow.com/questions/4903758/android-how-to-refresh-listview-contents. Since your adapter is List based, you could consider changing its type to ArrayAdapter (convenience implementation of BaseAdapter), which has add/addAll/remove methods just like List does. Call methods to clear/add/remove items from the adapter directly and when the content is updated, call notifyDataSetChanged to notify the adapter that the ListView needs to be updated. That way you don't need to maintain a List of items yourself. – 2Dee Jan 29 '15 at 12:37