getView() int keeps doubling every load

83 views Asked by At

I have this strange occurrence in my app. I am using the code I have list below to populate a TextView with the value of a counter. Every time the view gets refreshed the value of the counter double, I have set the counter to 0 after each cycle through but does not seem to work.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View itemView = convertView;
    if (itemView == null) {
        itemView = inflater.inflate(R.layout.layouttitle, container, false);
    }
    for (myStruct singleItem : myData) {
        counter++;
    }
    TextView totalsText = (TextView) itemView.findViewById(R.id.myTotalsText);
    totalsText.setText("" + counter);
    counter++;
}
1

There are 1 answers

6
Rahul On BEST ANSWER

getView() method of Adapter called multiple times and there is no guarantee how many times it get called, so better don't use this method for any calculations or fro any other task which is relevant to number of items in your ListView.

And why counter is a global variable, can you explain it.