2010-11-26 11 views
5

Cursorの情報をListViewに表示しようとしていますが、各行にImageViewTextViewが含まれています。私はCustomCursorAdapterCursorAdapterに広げて、bindViewで私はカーソルからデータを評価し、それに基づいてビューのイメージとテキストを設定します。CursorAdapterをオーバーライドする方法bindView

ListViewは、アプリケーションを実行すると正しい行数が表示されますが、空です。私はbindViewをオーバーライドするときに何かを見逃してしまったことは知っていますが、私は何がわかりません。

ご協力いただければ幸いです。

private class CustomCursorAdapter extends CursorAdapter { 

    public CustomCursorAdapter() { 
    super(Lmw.this, monitorsCursor); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    LayoutInflater layoutInflater = getLayoutInflater(); 

    return layoutInflater.inflate(R.layout.row, null); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
    try { 
     int monitorNameIndex = cursor.getColumnIndexOrThrow(DbAdapter.MONITORS_COLUMN_MONITOR_NAME); 
     int resultTotalResponseTimeIndex = cursor.getColumnIndexOrThrow(DbAdapter.RESULTS_COLUMN_TOTAL_RESPONSE_TIME); 

     String monitorName = cursor.getString(monitorNameIndex); 
     int warningThreshold = cursor.getInt(resultTotalResponseTimeIndex); 

     String lbl = monitorName + "\n" + Integer.toString(warningThreshold) + " ms"; 

     TextView label = (TextView) view.findViewById(R.id.label);  
     label.setText(lbl); 

     ImageView icon = (ImageView)view.findViewById(R.id.icon); 
     if(warningThreshold < 1000) { 
     icon.setImageResource(R.drawable.ok);  
     } else { 
     icon.setImageResource(R.drawable.alarm); 
     } 


    } catch (IllegalArgumentException e) { 
     // TODO: handle exception 
    } 
    } 
} 
+0

これを後で試しますが、1つのコメント:コンストラクタでこれらの 'cursor.getColumnIndexOrThrow(DbAdapter.MONITORS_COLUMN_MONITOR_NAME);コールを実行する必要があります。あなたは 'Log.e'例外についてどうですか? –

答えて

4

bindView()のようです。パフォーマンス上の理由から

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    return mInflater.inflate(R.layout.row, parent, false); 
} 

そして、::

あなたnewViewを交換してみてください()メソッドすべてcursor.getColumnIndexOrThrow()呼び出しで コンストラクタ

  • 同じことで

    • 移動getLayoutInflater()、 として
      件の コメント
    • 持つInteger.toString(warningThreshold)を行う必要はありませんあなたのLBLテキスト
    • を作成するためのStringBuilderを使用して... はちょうどwarningThreshold

    後に編集を使用しますinflate()メソッドと私が提案したものの唯一の違いは、この1つはレイアウトパラメータを作成することです親を教えてください。

  • +0

    エラーは私のSQLで実際には、 'RESULTS_COLUMN_TOTAL_RESPONSE_TIME'列はカーソル内になかったことがわかります。パフォーマンスの問題に関するフィードバックをありがとう。あなたの答えを正しいものとしてマークします。 – Holm

    関連する問題