Incompatible return type within customList

2019-08-30 10:42发布

问题:

I've been having an issue. I've been trying to change the return type of getItem within my Custom List but this error keeps appearing: The return type is incompatible with ArrayAdapter.getItem(int). Can anyone help me?

Here's my Custom List's codes:

import java.io.File;
import java.net.URL;
import java.util.List;

import nyp.edu.sg.alumnigo.asynctask.GetEventsImageAsyncTask;


import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String>{

    private final Activity context;
    private final List<Event> eventsList;

    public CustomList(Activity context, List<Event> eventsList) {
        super(context, R.layout.list_single);
        this.context = context;
        this.eventsList = eventsList;


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(eventsList.size() <= 0)
        {
            return 0;
        }
        return eventsList.size();
    }

    @Override
    public Event getItem(int position) {
        // TODO Auto-generated method stub
        return eventsList.get(position);
    }


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        //set up the inflater...
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);

        //reference the widgets...
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        TextView txtDate = (TextView) rowView.findViewById(R.id.txt2);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        Log.i("CustomList", "Start customList");

        txtTitle.setText(eventsList.get(position).getEvent_title());
        txtDate.setText(eventsList.get(position).getStart_date());
        new GetEventsImageAsyncTask(imageView).execute(Constants.HOST_NAME + "/"+ Constants.CMS_NAME+ "/" +eventsList.get(position).getSmall_picture_path());

        Log.i("CustomList", "End customList");

        return rowView;
    }
}

回答1:

look carefullly at your code..here..

public class CustomList extends ArrayAdapter<String>{}

that is your declaration for your customList ArrayAdatper and you are trying to return an Event type from your String ArrayAdapter.. do you get it now? so it's return type is goin to String.. do this rather

public class CustomList extends ArrayAdapter<Event>{


回答2:

Since you extends ArrayAdapter with String generic type

you can only return a String type