5

の情報を表示しようとしてCursorいます。ListView各行にはとが含まれてImageViewTextViewます。私はCustomCursorAdapter拡張機能を持っています。カーソルからのデータを評価しCursorAdapterbindViewそれに基づいてビューの画像とテキストを設定します。

アプリを実行すると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
    }
  }
}
4

1 に答える 1

4

このbindView()方法は問題ないようです。

newView()メソッドを置き換えてみてください。

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

そして、パフォーマンス上の理由から:

  • getLayoutInflater()コンストラクター内を移動する
  • コメントですでに述べた ように、すべてのcursor.getColumnIndexOrThrow()呼び出しで同じこと
  • StringBuilderを使用してlblテキストを作成します
  • Integer.toString(warningThreshold)を実行する必要はありません... warningThresholdを使用するだけです

後で編集:あなたのメソッドと私が提案したメソッドの唯一の違いinflate()は、これが親に一致するレイアウトパラメータを作成することです。

于 2010-11-26T12:27:30.640 に答える