gpt4 book ai didi

Android ArrayAdapter getItem() 类型不兼容?

转载 作者:行者123 更新时间:2023-11-29 16:45:37 24 4
gpt4 key购买 nike

我很难尝试让我的 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); 作为不兼容的类型(下图):

enter image description here

我卡住了:/你知道我的代码有什么问题吗?提前致谢!

最佳答案

这应该可以工作

Testowa 更改为 -> WeekViewEvent

public TEventAdapter (Context上下文,ArrayList<Testowa>testowyeventarray)

公共(public)类 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;
}
}

关于Android ArrayAdapter getItem() 类型不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48545673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com