سؤال

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...

Guide.getSessionsByTrack(...);

...means you are attempting to reference a method by it's parent class name rather than via a referenced (and instantiated) instance of a Guide object. In this case, yes, the method must be declared as static because you're not explicitly instantiating an instance of the Guide class.

The second problem you have is that getSessionsByTrack(...) isn't actually a method that belongs to the Guide class itself, rather it belongs to a private inner class (Session). Basically this method isn't reachable anyway.

You need to fix both of those before it will work. Either create a public static method in your Guide class which in itself calls a static method in the Session class or create an instance of Guide and provide a similar get method that can be accessed publicly.

Also you seem to have a misunderstanding about static in that you think it means things cannot be updated. That would be final (in other words 'constant') the use of static has a different meaning. I'd suggest you read up on the Java final and static usage.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top