2
My code is as follows: in the method getView of my BaseAdapter, i add all created views to a list (I need this to do a certain thing). In that same Basedapter, I have a method getQuantViews that returns how many views were added to that list.
In my Ragment, I use the listview.setAdapter to create the views and then I call such a method getQuantViews of my BaseAdapter to find out how many views were added and display this in a Log.
When loading the ListView, I notice in the log that the order of the processes is not happening correctly, because in the log, is first called the getQuantViews and after you enter the getView.
Did I make a mistake in the order of the proceedings? For, in my view, he would first enter the getView when using the setAdapter and only after that would he call the getQuantViews. I also tried to separate the processes by putting the setAdapter in the onStart and then call the getQuantViews in the onResume, but the result was the same.
In the Base Adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Trial t = listTrial.get(position);
View layout;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.item_trial, null);
} else {
layout = convertView;
}
DecimalFormat df = new DecimalFormat("####.00");
linkViews(layout);
tv_trial_name.setText(t.getName());
tv_trial_date.setText(t.getDate());
tv_trial_hour.setText(t.getHour());
tv_peak_force.setText(
layout.getResources().getString(R.string.text_peak_force) + ": " + df.format(t.getPeakForce()));
tv_comments.setText(t.getComment());
this.listViews.add(layout);
Log.v("Bio", "listViews size: " + this.listViews.size());
return layout;
}
public int getQuantViews() {
return this.listViews.size();
}
No Fragment:
@Override
public void onStart() {
loadTrials();
super.onStart();
Log.v("Bio", "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.v("Bio", "onResume");
check();
}
The loadTrials method arrow the basedapter:
private void loadTrials() { tAdapter = new TrialAdapter(view.getContext(), list_trials); lv_trials.setAdapter(tAdapter); }The check method shows the amount of views in the list
public void check() { Log.v("Bio", "Retorno: " + tAdapter.getQuantViews()); }
The question is whether the
onResumeshould rotate before theonStart? I don’t think so, it doesn’t happen because of the life cycle of Activity. In addition to thegetViewis called byListViewand this is done in some of the first steps of layout (I do not guarantee that it is the first).– Wakim
No, the doubt is because it is calling . getQuantViews() if before it would have to go through the Getview of Basedapter to create the layouts
– Marcos Vinícius
The method
getView()of Adapter is only called when the Listview needs to do the display of a line on the screen and not when the setAdapter() is called.– ramaral
then how would I call my method getQuantViews after all items were displayed?
– Marcos Vinícius
Even not knowing what you want to do with this list I suppose, fill it in the method
getView(), will not do what you want since it is called whenever it is necessary to do the display of a line. Whenever it is done scroll the method will be called and new items will be added, it will grow indefinitely and will have repeated items.– ramaral