méthode statique requise pour adaptateur listview personnalisé
-
27-10-2019 - |
Question
J'ai une classe d'objet (Guide) avec une sous-classe remplaçant (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;
}
J'ai un ListAdapter qui devrait gérer la liste pour afficher une listview 2 lignes:
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;
}
}
Dans mon activité principale, je suis en train d'afficher la liste en utilisant l'adaptateur de liste:
...
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);
...
Mon seul problème dans la Guide.getSessionsByTrack méthode ne me permet pas d'utiliser this.sessions alors cette méthode est statique. Faut-il que SessionList être statique, si je voulais mettre à jour la liste, ne doit pas être statique ce pas?
Ce petit accident de parcours est la seule chose qui me empêche de mon but et toute aide serait grandement appréciée.
La solution
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.