gpt4 book ai didi

java - 无法解析方法 'getEvent()'

转载 作者:行者123 更新时间:2023-12-02 02:03:23 25 4
gpt4 key购买 nike

我正在为我的自定义 ListView 制作一个适配器类。但是,当尝试从我的 Events 类中检索时,它显示“无法解析方法 'getEvent()'” 我该如何解决此问题?它围绕着我试图从中检索的 3 种方法。 getEvent、getDate 和 getTime。这是我的适配器类:

public class EventsListAdapter extends ArrayAdapter<Events> {

private Context mContext;
int mResource;


public EventsListAdapter(@NonNull Context context,@LayoutRes int resource, @NonNull ArrayList<Events> objects) {
super(context, resource, objects);

mContext = context;
mResource = resource;
}


public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

String event = getItemId(position).getEvent();
String date = getItemId(position).getDate();
String time = getItemId(position).getTime();

Events events = new Events(event,date,time);

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

TextView tvEvent = (TextView)convertView.findViewById(R.id.textView11);
TextView tvDate = (TextView)convertView.findViewById(R.id.textView21);
TextView tvTime = (TextView)convertView.findViewById(R.id.textview31);

tvEvent.setText(event);
tvDate.setText(date);
tvTime.setText(time);

return convertView;
}
}

这是我的事件类:

public class Events {

private String event;
private String date;
private String time;

public Events(String event, String date, String time){
this.event = event;
this.date = date;
this.time = time;

}

public String getEvent() {
return event;
}

public void setEvent(String event) {
this.event = event;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}
}

最佳答案

问题出在您的 getView 方法中:

    String event = getItemId(position).getEvent();
String date = getItemId(position).getDate();
String time = getItemId(position).getTime();

作为文档,getItemId 返回一个 long:

long getItemId(int position)

Get the row id associated with the specified position in the list.

这就是您收到错误的原因:您无法在 long 上调用 getEvent

您可能正在寻找方法getItem,该方法通过给出您想要获取的元素:

T getItem(int position)

Get the data item associated with the specified position in the data set.

https://developer.android.com/reference/android/widget/ArrayAdapter

关于java - 无法解析方法 'getEvent()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51229784/

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