2016-05-26 6 views
-2

特定の条件がtrueであることに基づいて、リストビューで特定の行をハイライトしたいと思います。次のコードを試して、NULLポインタ例外が発生しました。特定の条件がtrueの場合、リストビューの行の色を変更しますか?

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誰でこれらのnullポインタ例外が説明し得ます。 何か助けや助言を歓迎します。ありがとうございます。

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

私はすべての希望:

+0

は、 'equalsIgnoreCase'の代わりに' equals'を使用して、やり直してください。 –

+0

@vrundpurohit私は等しくしようとしましたが、何の違いもありませんでした。 – AndroidNewBee

+0

if(status [position] .compareTo( "true")== 0) { listViewItem.setBackgroundColor()... } – Nanoc

答えて

1

あなたはあなたが背景を変更する前に、アレイ上のアレイの位置を確認してくださいそれが

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

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

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

これを試しましたが、最後の行だけを変更したときに最初と最後の行の色が変わりますステータスはtrueです。 – AndroidNewBee

+0

@AndroidNewBee私の答えを編集してください。 – mdDroid

+0

ありがとう、@mdDroid。 – AndroidNewBee

0

を次のようにコードを変更避けるために、いくつかの位置のためにgettign status[position] NULLであるかもしれませ配列が同じ長さであるか、ArrayAdapterロジックが正常に動作しない

1

よく@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); 

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

happyCoding以下のような条件を書き込むことによって、文字列ためNPEを回避する最善の方法。

関連する問題