【问题标题】:Incompatible return type within customListcustomList 中的返回类型不兼容
【发布时间】:2014-10-13 00:34:35
【问题描述】:

我遇到了问题。我一直在尝试更改自定义列表中 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;
    }
}

【问题讨论】:

  • 它为您提供的将返回类型更改为的替代方法是什么?

标签: android custom-lists


【解决方案1】:

仔细查看您的代码..这里..

public class CustomList extends ArrayAdapter<String>{}

这是您对 customList ArrayAdatper 的声明,您正在尝试返回 来自您的 String ArrayAdapter 的事件类型。您现在明白了吗?所以它的返回类型是 String.. 而是这样做

public class CustomList extends ArrayAdapter<Event>{

【讨论】:

    【解决方案2】:

    既然你用 String 泛型类型扩展了 ArrayAdapter

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

    【讨论】:

      猜你喜欢
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2014-08-29
      • 2016-05-28
      • 2012-08-06
      • 2016-02-01
      相关资源
      最近更新 更多