0

I am using BaseAdapter for displaying some items.

I don't want to change the text for item at position zero.

So getView method of my BaseAdapter looks like

public View getView(int position, View view, ViewGroup parent) {
    try {
        if(view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_bill, parent, false);
        }
        Log.e("BillAdapter", "Position :: " + position);
        if(position == 0) {
            Log.e("BillAdapter", "Position zero. Not changing values....");
        } else {
            Bill bill = items.get(position);
            Log.e("BillAdapter", position + " :: " + bill.toString());
            TextView textDate = (TextView) view.findViewById(R.id.text_date);
            textDate.setText(bill.getDate());
            TextView textPaidAmount = (TextView) view.findViewById(R.id.text_paid_amount);
            textPaidAmount.setText(String.format("%2.2f", bill.getPaidAmount()));
        }
    } catch (Exception ex) {
        Log.e("BillAdpater", Log.getStackTraceString(ex));
    }
    return view;
}

But my output is

Output

I'm using the same logic in my all applications and it works. I don't know whats wrong in this.

It reflects the last value at first position.

I tried to rebuild the project, clean the project, but no success.

Thanks in advance.

ELITE
  • 5,815
  • 3
  • 19
  • 29

3 Answers3

1

your problem is ListView's recycling mechanism. please read this to get more info.

you can solve your problem with setting data in if statement, so your getView must be like :

try {
        if(view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_bill, parent, false);
        }
        Log.e("BillAdapter", "Position :: " + position);
        TextView textDate = (TextView) view.findViewById(R.id.text_date);
        TextView textPaidAmount = (TextView) view.findViewById(R.id.text_paid_amount);

        if(position == 0) {
            textDate.setText("Date");
            textPaidAmount.setText("Paid Amount");
        } else {
            Bill bill = items.get(position);
            Log.e("BillAdapter", position + " :: " + bill.toString());
            textDate.setText(bill.getDate());

            textPaidAmount.setText(String.format("%2.2f", bill.getPaidAmount()));
        }
    } catch (Exception ex) {
        Log.e("BillAdpater", Log.getStackTraceString(ex));
    }

when you set one info in if statement in listView you MUST set default or another value in else too, if you don't set that as recycling mechanism happened your value changed with one random position after scrolling.

EDIT :: Refer image for detailed understanding

Details

it's better to use ViewHolder pattern in your adapter class: you can use following code.

in getView use :

 try {

       ViewHolder viewHolder;

       if(view == null) {
           view = LayoutInflater.from(context).inflate(R.layout.item_bill, parent, false);
           viewHolder = new ViewHolder();
           viewHolder.textPaidAmount = (TextView) view.findViewById(R.id.text_paid_amount);
           viewHolder.textDate =  (TextView) view.findViewById(R.id.text_date);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

            Log.e("BillAdapter", "Position :: " + position);

            if(position == 0) {
                holder.textDate.setText("Date");
                holder.textPaidAmount.setText("Paid Amount");
            } else {
                Bill bill = items.get(position);
                Log.e("BillAdapter", position + " :: " + bill.toString());
                holder.textDate.setText(bill.getDate());
                holder.textPaidAmount.setText(String.format("%2.2f", bill.getPaidAmount()));
            }
        } catch (Exception ex) {
            Log.e("BillAdpater", Log.getStackTraceString(ex));
        }

and ViewHolder class is :

class ViewHolder {
    TextView textDate;
    TextView textPaidAmount;
}
Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • Thanks a lot..It solved my problem and also I got the logic behind why it returns the last value in first position...thanks a lot.. – ELITE May 22 '16 at 07:01
  • it's better to use ViewHolder pattern in adapter class, i will edit code with ViewHolder pattern , – Shayan Pourvatan May 22 '16 at 07:02
1

The View parameter is a reused instance. Android reuses for performance reasons.

You should instead set the expected values explicitly when position ==0.

Niels
  • 725
  • 5
  • 14
0

I also faced this problem,

   In position 0 pass textView = "" 
Manmohan Kumar
  • 334
  • 2
  • 8