2010-06-04 46 views
3
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); 
} 
} 

回答

0

如果採取「視圖」,會發生什麼情況。離開並引用外部類的findViewById?

+0

對不起,我想通了。在newView()方法中,id是錯誤的...謝謝。 – vycon 2010-06-05 04:24:42