ListView只有在android中滚动后才会更新

时间:2014-07-14 15:50:45

标签: android listview scroll baseadapter

下面是我的listview适配器的代码: -

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中的颜色。我希望每次在屏幕上显示listview时显示颜色。 有人请告诉我该怎么办,所以它会按照预期行事吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您使用ViewHolder模式,则无需在findViewById方法的每次调用中调用getView getView 的第二个参数是将旧视图重用。仅当此旧视图为null时,您才可以创建新视图,填充它并存储在布局中。

@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;

}

<强>更新

还以正确的方式实现getItem方法:

@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?
}

我会尝试先使用一些固定颜色 - 例如Colors.RED来查找代码背景中未正确设置的位置