Doubt about the operation of Basedapter and Listview

Asked

Viewed 335 times

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 onResume should rotate before the onStart? I don’t think so, it doesn’t happen because of the life cycle of Activity. In addition to the getView is called by ListView and this is done in some of the first steps of layout (I do not guarantee that it is the first).

  • 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

  • 1

    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.

  • then how would I call my method getQuantViews after all items were displayed?

  • 2

    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.

1 answer

1

You can use the getCount method of the Adapter base and check that the current position of getView is equal to the value returned by getCount, if it is the same you will have reached the end of the list. This comparison would be made within getView itself where vc could be displaying the log according to your need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.