Search code examples
javaandroidandroid-fragmentssimpleadaptergetview

Can't wirte getView in my SimpleAdapter in Fragment


I have a fragment with a AsyncTask in which I do a parsing Json. To put every element in a list I use a SimpleAdapter. In my item list I have a button. Of course, if I want click on the button I need to create a getView. Well not works. This is the SimpleAdapter part inside the AsyncTask

ListAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.list_rom_item, 
                  new String[] { TAG_NAME, TAG_SURNAME }, new int[] { R.id.name, R.id.surname}); 

Now, after that, I tried to put this:

                 @Override
                 public View getView (int position, View convertView, ViewGroup parent)
                 {
                     View v = super.getView(position, convertView, parent);

                      Button b=(Button)v.findViewById(R.id.mytask);
                      b.setOnClickListener(new OnClickListener() {

                         @Override
                         public void onClick(View arg0) {
                             // TODO Auto-generated method stub
                             Toast.makeText(MainActivity.this,"save",Toast.LENGTH_SHORT).show();
                         }
                     });
                     return v;
                 }

I get an error in @Override like: The annotation @Override is disallowed for this location and then of course in getView() syntax.. I don't know why. I tried also to clean the project but nothing. Is there any other solution maybe? Thanks.


Solution

  • You should override getView() method when instantiating your adapter. After that, you will not be able to do that.

    Try this way:

    ListAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.list_rom_item, 
                      new String[] { TAG_NAME, TAG_SURNAME }, new int[] { R.id.name, R.id.surname}) {
        @Override
        public View getView (int position, View convertView, ViewGroup parent) {
            ...
            return v;
        }
    }