1

我很难让我的 ArrayAdapter 工作 - 以前使用自定义 ArrayAdapter 没有问题,但这次我不确定发生了什么。

我正在尝试使用适配器创建 ListView,该适配器将显示每个 WeekViewEvent 对象的两个字符串(mName,mLocation):

public class WeekViewEvent implements Serializable{
private long mId;
private Calendar mStartTime;
private Calendar mEndTime;
private String mName;
private String mLocation;
private int mColor;
private boolean mAllDay;
private Shader mShader;
//construcotrs, setters, getters

阵列适配器:

  public class TEventAdapter extends ArrayAdapter<Testowa> {

    public TEventAdapter (Context context, ArrayList<Testowa> testowyeventarray){
        super(context, 0, testowyeventarray);}

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View pojedynczyItemView = convertView;
        if(pojedynczyItemView==null){
            pojedynczyItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        WeekViewEvent wvevent = getItem(position);

        TextView tv1 = (TextView) pojedynczyItemView.findViewById(R.id.tv_lv1);
        tv1.setText(wvevent.getName());
        TextView tv2 = (TextView) pojedynczyItemView.findViewById(R.id.tv_lv2);
        tv2.setText(wvevent.getLocation());

        return pojedynczyItemView;
    }
}

但 AndroidStudio 突出显示了我正在获取对象位置的部分:WeekViewEvent wvevent = getItem(position);作为不兼容的类型(下图):

在此处输入图像描述

我被卡住了:/你有什么想法我的代码有什么问题吗?先感谢您!

4

1 回答 1

3

这应该工作

更改Testowa为 -> WeekViewEvent

公共 TEventAdapter(上下文上下文,ArrayList< Testowa>testowyeventarray)

公共类 TEventAdapter 扩展 ArrayAdapter< Testowa>

public class TEventAdapter extends ArrayAdapter<WeekViewEvent> {

    public TEventAdapter (Context context, ArrayList<WeekViewEvent> weekVieweEvent){
        super(context, 0, weekVieweEvent);}

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View pojedynczyItemView = convertView;
        if(pojedynczyItemView==null){
            pojedynczyItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        WeekViewEvent wvevent = getItem(position);

        TextView tv1 = (TextView) pojedynczyItemView.findViewById(R.id.tv_lv1);
        tv1.setText(wvevent.getName());
        TextView tv2 = (TextView) pojedynczyItemView.findViewById(R.id.tv_lv2);
        tv2.setText(wvevent.getLocation());

        return pojedynczyItemView;
    }
}
于 2018-01-31T15:42:57.100 回答