0

サブクラス(セッション)を持つオーバーライドオブジェクトクラス(ガイド)があります。

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;
}

リストを処理して 2 行のリストビューを表示する ListAdapter があります。

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は静的でなければなりません。リストを更新したい場合、これは静的であってはいけませんか?

この小さなしゃっくりは、私を私の目標から遠ざける唯一のものであり、どんな助けも大歓迎です.

4

1 に答える 1

0

そこには2つの問題があります。

まずはこんなことを…

Guide.getSessionsByTrack(...);

...オブジェクトの参照(およびインスタンス化)されたインスタンスを介してではなく、親クラス名でメソッドを参照しようとしていることを意味しGuideます。この場合、はい、クラスstaticのインスタンスを明示的にインスタンス化していないため、メソッドを as として宣言する必要があります。Guide

2 つ目の問題は、実際にはクラス自体にgetSessionsByTrack(...)属するメソッドではなく、内部クラス ( ) に属していることです。基本的に、このメソッドはとにかく到達できません。GuideprivateSession

機能する前に、これらの両方を修正する必要があります。それ自体がクラス内のメソッドを呼び出すクラス内にpublic staticメソッドを作成するか、インスタンスを作成してパブリックにアクセスできる同様のメソッドを提供します。GuidestaticSessionGuideget

staticまた、更新できないという意味だと誤解されているようですね。それはfinal(言い換えれば「定数」)であろう の使用はstatic別の意味を持ちます。finalJavaと使用法についてよくお読みになることをお勧めしますstatic

于 2012-02-19T02:38:18.137 に答える