Doubt about the operation of BaseAdapter and ListView

2

My code is as follows: In the getView method of my BaseAdapter , I add all views created to a list (I need this to do a certain thing). In that same BaseAdapter, I have a getQuantViews method that returns how many views have been added to that list.

In my fragment, I use listview.setAdapter to create the views and then I call that method getQuantViews of my BaseAdapter to know how many views have been added and display this in a Log.

When I load ListView , I notice in the log that the order of the processes is not happening correctly, because in the log it is first called the getQuantViews and after entering getView .

Have I made a mistake in the order of the processes? For, in my view, it would first get into the getView when using the setAdapter and only after it would call the getQuantViews . I also tried to separate the processes by putting setAdapter in onStart and then calling getQuantViews , but the result was the same.

No 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 directs the baseadapter:

    private void loadTrials() {
        tAdapter = new TrialAdapter(view.getContext(), list_trials);
        lv_trials.setAdapter(tAdapter);
    }
    
  • The check method shows the number of views in the list

    public void check() {
        Log.v("Bio", "Retorno: " + tAdapter.getQuantViews());
    }
    
asked by anonymous 16.09.2015 / 14:42

1 answer

1

You can use the getCount method of the base adapter and check if the current position of the getView is equal to the value returned by the getCount, if you have reached the end of the list. This comparison would be done inside the getView itself where you could be displaying the log according to your needs.

    
16.09.2015 / 19:47