Android ArrayAdapter getItem() 类型不兼容?
Android ArrayAdapter getItem() incompatible types?
我很难尝试让我的 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);
作为不兼容的类型(下图):
我卡住了:/你知道我的代码有什么问题吗?
提前致谢!
这应该有效
将 Testowa
更改为 -> WeekViewEvent
public TEventAdapter (Context上下文,ArrayList<Testowa
>testowyeventarray)
public class 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;
}
}
我很难尝试让我的 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);
作为不兼容的类型(下图):
我卡住了:/你知道我的代码有什么问题吗? 提前致谢!
这应该有效
将 Testowa
更改为 -> WeekViewEvent
public TEventAdapter (Context上下文,ArrayList<Testowa
>testowyeventarray)
public class 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;
}
}