无法更改listview的背景颜色?

时间:2015-11-19 06:00:08

标签: android android-listview

我是Android开发的新手我试图改变listview的背景颜色但是它抛出空指针异常到目前为止我试过的是:

这是我的适配器customListAdapterldrequirements.java

public class customListAdapterldrequirements extends BaseAdapter{


    private ArrayList<LdNewsItem> listData;
    private LayoutInflater layoutInflater;

    public customListAdapterldrequirements(Context context, ArrayList<LdNewsItem> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position).getSbu();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        mview.setBackgroundColor(Color.parseColor("#E5E5E5"));
        if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_ld_requirements, null);
        holder = new ViewHolder();
        holder.headlineView = (TextView) convertView.findViewById(R.id.sbu);
        holder.reporterNameView = (TextView) convertView.findViewById(R.id.bu);
        holder.reportedDateView = (TextView) convertView.findViewById(R.id.sbuu);
        holder.ldstatusView = (TextView) convertView.findViewById(R.id.sc);
        holder.ldtype=(TextView)convertView.findViewById(R.id.ssc);
        holder.ldsource=(TextView)convertView.findViewById(R.id.rq);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.headlineView.setText("Strategic Business Unit: " + listData.get(position).getSbu());
    holder.reportedDateView.setText("Business Unit: "+listData.get(position).getSbuu());
    holder.ldstatusView.setText("Sub Business Unit: " +listData.get(position).getSc());
    holder.ldtype.setText("Service Category: " + listData.get(position).getSsc());
    holder.ldsource.setText("Service Sub Category: "+ listData.get(position).getReq());
   // holder.headlineView.setText("Requirements: " +listData.get(position).getReq());
    return convertView;
}


static class ViewHolder {
    TextView headlineView;
    TextView reporterNameView;
    TextView reportedDateView;
    public TextView ldstatusView;
    public TextView ldtype;
    public TextView ldsource;
    public TextView req;
}
}

这是我的代码我正在改变listview背景不起作用我该怎么改变?

1 个答案:

答案 0 :(得分:3)

移动

convertView.setBackgroundColor(Color.parseColor("#E5E5E5"));

之后

convertView = layoutInflater.inflate(R.layout.list_ld_requirements, null);

修正:

if (convertView == null) {
    convertView = layoutInflater.inflate(R.layout.list_ld_requirements, null);
  ..........
  convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}

 convertView.setBackgroundColor(Color.parseColor("#E5E5E5"));