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.