I have an overriding Object class (Guide) with a subclass (Session).
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;
}
I have a ListAdapter that should handle the list to display a 2-line listview:
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;
}
}
In my main activity I am trying to display the list using the list adapter:
...
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);
...
My only problem in the Guide.getSessionsByTrack method doesn’t allow me to utilize this.sessions while that method is static. Must the sessionList be static, what if I wanted to update the list, shouldn’t this not be static?
This little hiccup is the only thing keeping me from my goal and any help would be greatly appreciated.
You have two problems there.
Firstly doing something like this…
…means you are attempting to reference a method by it’s parent class name rather than via a referenced (and instantiated) instance of a
Guideobject. In this case, yes, the method must be declared asstaticbecause you’re not explicitly instantiating an instance of theGuideclass.The second problem you have is that
getSessionsByTrack(...)isn’t actually a method that belongs to theGuideclass itself, rather it belongs to aprivateinner class (Session). Basically this method isn’t reachable anyway.You need to fix both of those before it will work. Either create a
public staticmethod in yourGuideclass which in itself calls astaticmethod in theSessionclass or create an instance ofGuideand provide a similargetmethod that can be accessed publicly.Also you seem to have a misunderstanding about
staticin that you think it means things cannot be updated. That would befinal(in other words ‘constant’) the use ofstatichas a different meaning. I’d suggest you read up on the Javafinalandstaticusage.