我有一个带有子类(会话)的重写对象类(指南)。
public class Guide
private class Session
...
ArrayList<Session> sessions;
public ArrayList<Session> getSessionsByTrack(int n) {
ArrayList<Session> tracks = new ArrayList<Session>();
for (Session session : this.sessions) {
if (session.track == n) {
tracks.add(session);
}
}
Collections.sort(tracks); // sort by start and end time.
return tracks;
}
我有一个 ListAdapter 应该处理列表以显示 2 行列表视图:
public class SessionListAdapter extends BaseAdapter {
private ArrayList<Session> sessions;
//private Session[] sl;
private LayoutInflater mInflater;
public SessionListAdapter(Context context, ArrayList<Session> sl) {
sessions = sl;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return sessions.size();
}
public Object getItem(int position) {
return sessions.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.session_two_line_list, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.session_title);
holder.time = (TextView) convertView.findViewById(R.id.session_time);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(sessions.get(position).getTitle());
holder.time.setText(sessions.get(position).getTimeSpan());
return convertView;
}
static class ViewHolder {
TextView title;
TextView time;
}
}
在我的主要活动中,我试图使用列表适配器显示列表:
...
this.lv1 = (ListView) view.findViewById(R.id.SessionListView);
// get sessions
this.sessionList = Guide.getSessionsByTrack(0); // errors here and complains that this method must be static
final SessionListAdapter lv1adapter = new SessionListAdapter(this, this.sessionList);
lv1.setAdapter(lv1adapter);
...
我在Guide.getSessionsByTrack方法中的唯一问题不允许我使用this.sessions而该方法是静态的。sessionList必须是静态的,如果我想更新列表怎么办,这不应该是静态的吗?
这个小问题是唯一让我无法实现目标的事情,任何帮助都将不胜感激。