0

Hi below is the code for my listview adapter:-

public class ChartListAdapter extends BaseAdapter
{
    List<Income> income_List;
    List<Expence> expence_List;
    Context activityContext;
    int Type;
    float[] percentage;
    String[] Total;
    String[] title;

    public ChartListAdapter(Context context, List<Income> lst_income,List<Expence> lst_expences,int type, float[] perce,String[] total, String[] Title)
    {
        // TODO Auto-generated constructor stub
        activityContext = context ;
        income_List = lst_income;
        expence_List = lst_expences;
        Type = type;
        this.percentage = perce;
        this.Total = total;
        this.title = Title;     
    }


    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        if (Type == 1)
        {
            return income_List.size();
        }
        else if(Type == 2)
        {
            return expence_List.size();
        }
        else
        {
            return Total.length;
        }
    }

    @Override
    public Object getItem(int arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2)
    {
        // TODO Auto-generated method stub
        View view;
         MyViewHolder holder;
        LayoutInflater mInflater =  (LayoutInflater)
                activityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view  = mInflater.inflate(R.layout.list_chart_row, null);
        holder = new MyViewHolder();
        RelativeLayout rl_listrow = (RelativeLayout)view.findViewById(R.id.rl_listrow);
        holder.text_color = (TextView)view.findViewById(R.id.text_color);
        holder.tv_name = (TextView)view.findViewById(R.id.text_name);
        holder.tv_amount = (TextView)view.findViewById(R.id.text_amount);
        holder.tv_percentage = (TextView)view.findViewById(R.id.text_percentage);

        if(Type == 1){

            holder.text_color.setBackgroundColor(Globals.COLORS[position]); 

            holder.tv_name.setText(income_List.get(position).in_catagory);

            holder.tv_amount.setText(String.valueOf(precision(2 , Float.valueOf(income_List.get(position).in_amount))));

            holder.tv_percentage.setText(Float.toString(percentage[position])+"%");

        }
        else if(Type == 2)
        {       
            holder.text_color.setBackgroundColor(Globals.COLORS[position]);

            holder.tv_name.setText(expence_List.get(position).ex_name);

            holder.tv_amount.setText(String.valueOf(precision( 2,Float.valueOf(expence_List.get(position).ex_amount))));

            holder.tv_percentage.setText(Float.toString(percentage[position])+"%");
         }
        else
        {
            if(position == 0)
                holder.text_color.setBackgroundResource(R.color.zeroposition);
            else
                holder.text_color.setBackgroundResource(R.color.firstposition);

            holder.tv_name.setText(title[position]);

            holder.tv_amount.setText(String.valueOf(precision(2,Float.valueOf(Total[position]))));

            holder.tv_percentage.setText(Float.toString(percentage[position])+"%");
        }

        if (position % 2 == 1) 
        {
            rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White));  
        } else {
            rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White));  
        }
        return view;

    }

    public static Float precision(int decimalPlace, Float d) {

        BigDecimal bd = new BigDecimal(Float.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.floatValue();
      }

    static class MyViewHolder {
        TextView text_color;
        TextView tv_name;
        TextView tv_amount;
        TextView tv_percentage;
    }

}

Problem I am facing is that listviw shows color in text_color only after scrolling. I want to show color everytime listview displayed on screen. Will anybody please tell me what should I do, so it will behave as per expectations?? Thanks in advance.

  • when your adapter loads before scrolling, which block do your code enters? type==1 block or type==2 or the last else block? Not an answer, but this condition is not required. You are doing samething in both condition. ` if (position % 2 == 1) { rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White)); } else { rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White)); }` – Jimmy Jul 14 '14 at 16:14
  • hey thanks for that. . but this doesn't solved my problem – user3702484 Jul 14 '14 at 16:30
  • did you try youradapter.notifyDataSetChanged(); ? http://stackoverflow.com/q/4903758/3640637 – PedroHawk Jul 14 '14 at 17:16

1 Answers1

0

If you're using ViewHolder pattern you don't need to call findViewById in every invoke of getView method. Second argument of getView is holding old view to reuse. Only if this old view is null you can create new view, populate it and store inside the layout.

@Override
public View getView(int position, View convertView, ViewGroup group)
{
    View view = convertView;
    MyViewHolder holder;
    if(view == null) {//create new view only once

        LayoutInflater mInflater =  (LayoutInflater)
            activityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view  = mInflater.inflate(R.layout.list_chart_row, null);
        holder = new MyViewHolder();
        RelativeLayout rl_listrow = (RelativeLayout)view.findViewById(R.id.rl_listrow);
        holder.text_color = (TextView)view.findViewById(R.id.text_color);
        holder.tv_name = (TextView)view.findViewById(R.id.text_name);
        holder.tv_amount = (TextView)view.findViewById(R.id.text_amount);
        holder.tv_percentage = (TextView)view.findViewById(R.id.text_percentage);
        view.setTag(holder);

    } else { //reuse old view
        holder = (MyViewHolder) view.getTag();
    }

    //at this point view is either reused (scrolling) or newly created, now you can set up your components

    if(Type == 1){

        holder.text_color.setBackgroundColor(Globals.COLORS[position]); 

        holder.tv_name.setText(income_List.get(position).in_catagory);

        holder.tv_amount.setText(String.valueOf(precision(2 , Float.valueOf(income_List.get(position).in_amount))));

        holder.tv_percentage.setText(Float.toString(percentage[position])+"%");

    }
    else if(Type == 2)
    {       
        holder.text_color.setBackgroundColor(Globals.COLORS[position]);

        holder.tv_name.setText(expence_List.get(position).ex_name);

        holder.tv_amount.setText(String.valueOf(precision( 2,Float.valueOf(expence_List.get(position).ex_amount))));

        holder.tv_percentage.setText(Float.toString(percentage[position])+"%");
     }
    else
    {
        if(position == 0)
            holder.text_color.setBackgroundResource(R.color.zeroposition);
        else
            holder.text_color.setBackgroundResource(R.color.firstposition);

        holder.tv_name.setText(title[position]);

        holder.tv_amount.setText(String.valueOf(precision(2,Float.valueOf(Total[position]))));

        holder.tv_percentage.setText(Float.toString(percentage[position])+"%");
    }

    if (position % 2 == 1) 
    {
        rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White));  
    } else {
        rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White));  
    }
    return view;

}

UPDATE

also implement getItem method correct way:

@Override
public Object getItem(int position)
{
    if(Type == 1) {
       return income_List.get(position);
    }
    if(Type == 2) {
       return  return expence_List.get(position);
    }
    // else
    return ??? // <- what o you want to return otherwise?
}

I would try also using some fixed colors first - like Colors.RED to find where in your code background is not set correctly

marcus m
  • 83
  • 6
  • Hey, thanks for your share, but it's still not working. I tried but now it stopped showing color even after scrolling now. . – user3702484 Jul 14 '14 at 16:42