findviewbyid()返回null - 在自定义CursorAdapter bindview()中

时间:2010-06-04 15:26:54

标签: android listview

package edu.ohio.android.bmm.activity;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import edu.ohio.android.bmm.R;
import edu.ohio.android.bmm.db.BMMDBOpenHelper.Diary;

public class DiaryList extends BMMActivity {

 // Cursor for sqlite database
 private Cursor cursor;

 private LayoutInflater mInflater;

 // Icons bound to rows
 private Bitmap mild;
 private Bitmap moderate;
 private Bitmap severe;

 /***
  * 
  */
 @Override
 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  cursor = fetchAllDiary();
  this.setListAdapter(new DiaryListAdapter(this, cursor, true));
 }

 /**
  * 
  * @author vycon
  *
  */
 public class DiaryListAdapter extends CursorAdapter {

  /**
   * 
   * @param context
   * @param cursor
   * @param autoRequery
   */
  public DiaryListAdapter(Context context, Cursor cursor, boolean autoRequery) {
    super(context, cursor, autoRequery);
    mInflater = LayoutInflater.from(context);

    // icons bound to the rows
    mild = BitmapFactory.decodeResource(context.getResources(), R.drawable.mild);
    moderate = BitmapFactory.decodeResource(context.getResources(), R.drawable.moderate);
    severe = BitmapFactory.decodeResource(context.getResources(), R.drawable.severe);
   }

  /**
   * 
   */
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
   TextView tvStartTime = (TextView) view.findViewById(R.id.diary_list_row_dateAndTime);
   TextView tvDuration = (TextView) view.findViewById(R.id.diary_list_row_duration);
   TextView tvSymptom = (TextView) view.findViewById(R.id.diary_list_row_symptom);
   TextView tvMedication = (TextView) view.findViewById(R.id.diary_list_row_medication);

   ImageView ivMaxSeverity = (ImageView) view.findViewById(R.id.diary_list_row_maxSeverity);
   ImageView ivInitSeverity = (ImageView) view.findViewById(R.id.diary_list_row_initSeverity);

   String maxSeverity = cursor.getString(cursor.getColumnIndex(Diary.MAX_SEVERITY));
   String initSeverity = cursor.getString(cursor.getColumnIndex(Diary.INIT_SEVERITY));

   tvStartTime.setText(cursor.getString(cursor.getColumnIndex(Diary.START_TIME)));
   tvDuration.setText(cursor.getString(cursor.getColumnIndex(Diary.DURATION)));
   tvSymptom.setText(cursor.getString(cursor.getColumnIndex(Diary.SYMPTOM)));
   tvMedication.setText(cursor.getInt(cursor.getColumnIndex(Diary.MEDICATION_ID)));

   if (maxSeverity.equalsIgnoreCase("mild")) {
    ivMaxSeverity.setImageBitmap(mild);
   }
   if (maxSeverity.equalsIgnoreCase("moderate")) {
    ivMaxSeverity.setImageBitmap(moderate);
   }
   if (maxSeverity.equalsIgnoreCase("severe")) {
    ivMaxSeverity.setImageBitmap(severe);
   }
   if (initSeverity.equalsIgnoreCase("mild")) {
    ivInitSeverity.setImageBitmap(mild);
   }
   if (initSeverity.equalsIgnoreCase("moderate")) {
    ivInitSeverity.setImageBitmap(moderate);
   }
   if (initSeverity.equalsIgnoreCase("severe")) {
    ivInitSeverity.setImageBitmap(severe);
   }
  }

  /**
   * 
   */
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
   final View view = mInflater.inflate(R.layout.diary_list_view, parent, false);
   return view;
  }

 }

 /**
  * 
  */
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(l, v, position, id);
 }
}

1 个答案:

答案 0 :(得分:0)

如果你采取“观点”会发生什么。离开并引用外部类的findViewById?