0

code to custom list adapter view (this list has only 11 items) and viewholder is not helping here.

@Override 
public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater  = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if( convertView == null ){ // for fast view google io http://www.youtube.com/watch?v=wDBM6wVEO70
    convertView = inflater.inflate(R.layout.single_transaction,parent,false);
    }
    TextView phone_number = (TextView) convertView.findViewById(R.id.phone_number_show);
    TextView transaction_summary = (TextView) convertView.findViewById(R.id.transaction_summary_show);
    TextView transaction = (TextView) convertView.findViewById(R.id.transaction_show);

    Transactions single_transaction = (Transactions)  getItem(position);

    phone_number.setText(String.valueOf(single_transaction.get_phone_number()));
    transaction_summary.setText(single_transaction.get_transaction_summary());

    int single_transaction_value = single_transaction.get_transaction();

    if(single_transaction_value < 0) {

        transaction.setBackgroundColor(Color.parseColor("#098AAA"));
    }

    transaction.setText(String.valueOf(single_transaction_value));

    return convertView;

}

here if condition alters default color(skyblue) of textview defined in xml
so if single_transaction_value < 0 color of textview changes to #098AAA(light green)
color is not changing as expected .

Note that color of top bar is correct. grey color for phone number. text on white background . skyblue or lightgreen color of transaction depending on if condition. Strange behaviour is in color changing from default skyblue defined in xml to lightgreen defined in code.

Now here comes a little change in if condition, added an else

if(single_transaction_value < 0) {

        transaction.setBackgroundColor(Color.parseColor("#098AAA"));
    }
else {
        transaction.setBackgroundColor(Color.parseColor("#76BB51"));
    }

now color is changing as expected.

how is this..

Cœur
  • 37,241
  • 25
  • 195
  • 267
devprashant
  • 1,285
  • 1
  • 13
  • 23
  • Screenshots before change http://s10.postimg.org/6vpcm6y95/device_2014_05_31_163612.png http://s29.postimg.org/6a01t9mvr/device_2014_05_31_163700.png – devprashant May 31 '14 at 11:24
  • Screenshots after change http://s28.postimg.org/ytdvo16ql/device_2014_05_31_170019.png http://s4.postimg.org/yyimc6ijh/device_2014_05_31_170005.png – devprashant May 31 '14 at 11:31
  • Because you are recycling `View`s so once the color changes to `#098AAA` you weren't changing it back to `#76BB51` when `single_transaction_value >= 0` before, so it stayed the same. Now since you are changing it you get the correct color – Apoorv May 31 '14 at 11:33
  • See first screenshot from after and before , then second screenshot from both. Note the lines with green and blue color.Issue is different. – devprashant May 31 '14 at 11:35
  • try initializing view by creating a View Holder Class.that will work – Ravi Kant May 31 '14 at 11:48
  • View Holder works for performance in scrolling listview. What happening here is view is allocating (sometimes, yes yes sometimes) wrong color to background as you can see in the screen shots. This list only contains 11 items. @apoorv – devprashant May 31 '14 at 12:02
  • first try ViewHolder,It will work – Ravi Kant May 31 '14 at 12:15

0 Answers0