如果某个条件为真,则更改列表视图中的行颜色?

Change row color in a listview if a certain condition is true?

我想根据特定条件突出显示列表视图中的特定行 true.I 尝试了以下代码,我得到了空指针异常。

public class BillsCustomList extends ArrayAdapter<String>
{
    private String[] doc_no;
    private String[]date;
    private String[] cust_name;
    private String[] cust_number;
    private String[] item_count;
    private String[]total_wt;
    private String[]total;
    private String[]balance;
    private String[]bill_type;
    private String[]status;

    private Activity context;

    public BillsCustomList(Activity context, String[] doc_no, String[] date, String[] cust_name,String[] cust_number,
                           String[] item_count,String[] total_wt,String[] total,String[] balance,String[] bill_type,String[] status)
    {
        super(context, R.layout.bills_list_view, doc_no);
        this.context =context;
        this.doc_no=doc_no;
        this.date = date;
        this.cust_name = cust_name;
        this.cust_number = cust_number;
        this.item_count= item_count;
        this.total_wt = total_wt;
        this.total = total;
        this.balance = balance;
        this.bill_type = bill_type;
        this.status = status;

    }

    @Override
    public View getView(int position, View listViewItem, ViewGroup parent)
    {
        if (null == listViewItem) {
            LayoutInflater inflater = context.getLayoutInflater();
            listViewItem = inflater.inflate(R.layout.bills_list_view, null, true);
        }
        TextView textViewDocno = (TextView) listViewItem.findViewById(R.id.textViewInvNo);
        TextView textViewDt = (TextView) listViewItem.findViewById(R.id.textViewDt);
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
        TextView textViewNumber = (TextView) listViewItem.findViewById(R.id.textViewNumber);
        TextView textViewCount = (TextView) listViewItem.findViewById(R.id.textViewCount);
        TextView textViewTotwt = (TextView) listViewItem.findViewById(R.id.textViewTotwt);
        TextView textViewTot = (TextView) listViewItem.findViewById(R.id.textViewTot);
        TextView textViewBal = (TextView) listViewItem.findViewById(R.id.textViewBalanace);
        TextView textViewBt = (TextView) listViewItem.findViewById(R.id.textViewBt);
        TextView textViewStatus = (TextView) listViewItem.findViewById(R.id.textView_status);
        LinearLayout ll = (LinearLayout) listViewItem.findViewById(R.id.ll_bills);

        textViewDocno.setText(doc_no[position]);
        textViewDt.setText(date[position]);
        textViewName.setText(cust_name[position]);
        textViewNumber.setText(cust_number[position]);
        textViewCount.setText(item_count[position]);
        textViewTotwt.setText(total_wt[position]);
        textViewTot.setText(total[position]);
        textViewBal.setText(balance[position]);
        textViewBt.setText(bill_type[position]);
        textViewStatus.setText(status[position]);

        try
        {
            if (textViewStatus.getText().toString().equalsIgnoreCase("true"))
            {
                ll.setBackgroundColor(Color.RED);

            }

        }catch (NullPointerException e)
        {
            Log.d("NULL ERROR", "position"+position+e);
        }




        return listViewItem;
    }


}

I have simple use case if the status is true make the row of a certain color.Else don't do anything.I get the following error when I try the above code.

NULL ERROR: position0java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference

我在 0 和 1 处遇到这些空指针异常 position.Can 谁能解释为什么会发生这种情况以及正确的方法是什么。 任何帮助或建议都是welcome.Thank你。

可能是某些职位的 status[position] 为空,以避免像下面这样更改您的代码

if(textViewStatus.getText()!=null){

     if (textViewStatus.getText().toString().equalsIgnoreCase("true"))
     {
        ll.setBackgroundColor(Color.RED);

     }else{
        ll.setBackgroundColor("Different color");
     }
}else{
      ll.setBackgroundColor("Different color");
}

您应该在更改背景之前检查阵列在阵列上的位置:

if (position < status.length() && status[position].equalsIgnoreCase("true"))
    ll.setBackgroundColor(Color.RED);
else
    ll.setBackgroundResource(android.R.color.transparent);

我希望所有数组的长度都相同,否则 ArrayAdapter 逻辑无法正常工作

嗯,@mdDroid 已经回答了问题,但是还有一些其他标准方法来检查空字符串。

TextUtils.isEmpty(CharSequence str) - Returns true if the string is null or 0-length.

if (!TextUtils.isEmpty(textViewStatus.getText()) && textViewStatus.getText().toString().equalsIgnoreCase("true"))
    ll.setBackgroundColor(Color.RED);
else
    ll.setBackgroundResource(android.R.color.transparent);

避免 字符串 NPE 的最佳方法 编写如下条件

 if ("true".equalsIgnoreCase(textViewStatus.getText().toString()))
     ll.setBackgroundColor(Color.RED);
 else
     ll.setBackgroundResource(android.R.color.transparent);