【发布时间】:2014-07-14 15:50:44
【问题描述】:
您好,下面是我的列表视图适配器的代码:-
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;
}
}
我面临的问题是 listviw 仅在滚动后才在 text_color 中显示颜色。我想每次在屏幕上显示列表视图时都显示颜色。 有人能告诉我我该怎么做,所以它会按预期运行吗? 提前致谢。
【问题讨论】:
-
当您的适配器在滚动之前加载时,您的代码进入了哪个块? type==1 块或 type==2 还是最后一个 else 块?不是答案,但不需要此条件。你在这两种情况下都在做同样的事情。 ` if (位置 % 2 == 1) { rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White)); } else { rl_listrow.setBackgroundColor(activityContext.getResources().getColor(R.color.White)); }`
-
嘿,谢谢。 .但这并没有解决我的问题
-
你试过 youradapter.notifyDataSetChanged(); ? stackoverflow.com/q/4903758/3640637
标签: android listview scroll baseadapter