customList中不兼容的返回类型

时间:2014-10-13 00:34:36

标签: android custom-lists

我一直有问题。我一直在尝试在自定义列表中更改getItem的返回类型,但此错误不断出现:返回类型与ArrayAdapter.getItem(int)不兼容。 任何人都可以帮助我吗?

这是我的自定义列表代码:

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

2 个答案:

答案 0 :(得分:1)

仔细查看你的代码......其中..

public class CustomList extends ArrayAdapter<String>{}

这是您对customList ArrayAdatper的声明,并且您正在尝试返回 你的String ArrayAdapter中的一个事件类型..你现在得到它吗?所以它的返回类型是goin to String ..执行此操作

public class CustomList extends ArrayAdapter<Event>{

答案 1 :(得分:0)

由于您使用String泛型类型

扩展了ArrayAdapter

你只能返回一个字符串类型