2011-07-09 9 views
3

私はsimplecursoradapterを拡張するアダプタを持っています。新しいビューでは、画像とともにデータベースからカーソルを取得し、いくつかのチェックボックスを使用してリストを作成します。何らかの理由で私が見ることができない、私のgetViewは呼び出されていない。 getView内にブレークポイントがあり、そこには決して到達せず、リストは空のままになります。 誰もがスルーを見て、私が間違ってgetViewは呼び出されませんか?

public class TakeStudentAttendance extends ListActivity { 
private gradeBookDbAdapter mDbHelper; 
private Long mRowId; 
private TextView mNameText; 
private String classname; 
private Boolean new_attendance = false; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Cursor stud; 

    mDbHelper = new gradeBookDbAdapter(this); 
    mDbHelper.open(); 
    mRowId = (savedInstanceState == null) ? null 
      : (Long) savedInstanceState 
        .getSerializable(gradeBookDbAdapter.KEY_ROWID); 
    if (mRowId == null) { 
     Bundle extras = getIntent().getExtras(); 
     mRowId = extras != null ? extras 
       .getLong(gradeBookDbAdapter.KEY_ROWID) : null; 
    } 
    // pull in class data 
    stud = mDbHelper.fetchClass(mRowId); 
    startManagingCursor(stud); 

    classname = stud.getString(
       stud.getColumnIndexOrThrow(gradeBookDbAdapter.KEY_CLASSNAME)); 
    String title = "Attendance for " + classname; 
    setTitle(title); 

    setContentView(R.layout.attendance_list); 
    Button doneButton = (Button) findViewById(R.id.Done); 
    doneButton.setOnClickListener(mAttendanceActivity); 

    // check previous attendance date 
    String prevdate = stud.getString(
      stud.getColumnIndexOrThrow(gradeBookDbAdapter.KEY_PREVDATE)); 

    stud = mDbHelper.fetchAttendanceByClass(mRowId); // this query yields _id, name, 
                 // attend, late, dtime 

    if (mDbHelper.getClassDate() == prevdate){ 
     // previous date is the same, so we're doing attendance again: retain values 
     new_attendance = false; 
    } 
    else { 
     // dates are different, so we're starting from scratch and all students are 
     // absent until counted present. I just need names and will populate attendance 
     new_attendance = true;   
     // upon attendance start-up, NO ONE is present. Set all entries in DB to not present (0) 
     setNoAttend(stud, mRowId); 
     // reset cursor position 
     stud.moveToFirst(); 
    } 



    // Create an array to specify the fields we want to display in the list 
    String[] from = new String[]{gradeBookDbAdapter.KEY_NAME, 
           gradeBookDbAdapter.KEY_ROWID, 
           gradeBookDbAdapter.KEY_ATTEND, 
           gradeBookDbAdapter.KEY_LATE, 
           gradeBookDbAdapter.KEY_DTIME}; 

    // and an array of the fields we want to bind those fields to (in this case just text1) 
    int[] to = new int[]{R.id.stuname, 
         R.id.stuIndex, 
         R.id.attend, 
         R.id.late, 
         R.id.stuIndex}; 

    // Now create a simple cursor adapter and set it to display 
    // mRowId holds the class index. 
    MyDataAdapter studs = 
     new MyDataAdapter(this, R.layout.show_attendance, stud, from, to, mRowId, new_attendance); 
    setListAdapter(studs); 
} 

をやったかを見ることができますここに私のアダプタのコードは次のとおりです。

public class MyDataAdapter extends SimpleCursorAdapter { 
private Cursor c; 
private Context context; 
private Long classnum; 
private gradeBookDbAdapter mDbHelper; 
private Boolean newValues; 
private ArrayList<String> list = new ArrayList<String>(); 
private ArrayList<Boolean> itemCheckedHere = new ArrayList<Boolean>(); 
private ArrayList<Boolean> itemCheckedLate = new ArrayList<Boolean>(); 
private ArrayList<Integer> itemCheckedIdx = new ArrayList<Integer>(); 
int idxCol; 
int idx; 

// itemChecked will store the position of the checked items. 

public MyDataAdapter(Context context, int layout, Cursor c, String[] from, 
     int[] to, Long mRowId, Boolean new_attendance) { 
    super(context, layout, c, from, to); 
    this.c = c; 
    this.context = context; 
    mDbHelper = new gradeBookDbAdapter(context); 
    mDbHelper.open(); 
    classnum = mRowId; 
    newValues = new_attendance; 
    c.moveToFirst(); 
    for (int i = 0; i < c.getCount(); i++) { 
     itemCheckedHere.add(i, false); // initializes all items value with false 
     itemCheckedLate.add(i, false); // initializes all items value with false 
    } 
} 

public View getView(final int pos, View inView, ViewGroup parent) { 
    File file; 
    ImageView studentPhoto; 

    if (inView == null) { 
       LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inView = inflater.inflate(R.layout.show_attendance, null); 
    } 

    // set up name field   
    final TextView studentName = (TextView) inView.findViewById(R.id.stuname); 
    final TextView studentIndex = (TextView) inView.findViewById(R.id.stuIndex); 
     if (studentName != null) 
     { 
      c.moveToPosition(pos); 
      int index = c.getColumnIndex(gradeBookDbAdapter.KEY_NAME); 
      String name = c.getString(index); 
      studentName.setText(name); 

      index = c.getColumnIndex(gradeBookDbAdapter.KEY_STUDENT); 
      String Index = c.getString(index); 
      studentIndex.setText(Index); 


      // set up photo icon 

      file = new File(Environment.getExternalStorageDirectory() + 
         "/gradeBook/" + name + ".jpg"); 
      studentPhoto = (ImageView) inView.findViewById(R.id.icon); 

      if (file.exists()) { 
       String fileName = file.getAbsolutePath(); 
       BitmapFactory.Options opts = new BitmapFactory.Options(); 
       Bitmap bm; 

       bm = BitmapFactory.decodeFile(fileName, opts); 
       studentPhoto.setImageBitmap(bm); 
      } 
      else { 
       // use icon image 
       studentPhoto.setImageResource(R.drawable.person_icon); 
      } 

    } 
    final CheckBox cBoxH = (CheckBox) inView.findViewById(R.id.attend); 
    final CheckBox cBoxL = (CheckBox) inView.findViewById(R.id.late); 


    cBoxH.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      CheckBox cb = (CheckBox) v.findViewById(R.id.attend); 

      if (cb.isChecked()) { 
       itemCheckedHere.set(pos, true); 
       int Index = new Integer(studentIndex.getText().toString()); 
       mDbHelper.insertAttend(Index, classnum, 1); 
      } else if (!cb.isChecked()) { 
       itemCheckedHere.set(pos, false); 
       int Index = new Integer(studentIndex.getText().toString()); 
       mDbHelper.deleteAttend(Index, classnum); 
      } 
     } 
    }); 
    cBoxL.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      CheckBox cb = (CheckBox) v.findViewById(R.id.late); 

      if (cb.isChecked()) { 
       itemCheckedLate.set(pos, true); 
       // do some operations here 
      } else if (!cb.isChecked()) { 
       itemCheckedLate.set(pos, false); 
       // do some operations here 
      } 
     } 
    }); 
    cBoxH.setChecked(itemCheckedHere.get(pos)); // this will Check or Uncheck the 
    cBoxL.setChecked(itemCheckedLate.get(pos)); // this will Check or Uncheck the 
    // CheckBox in ListView 
    // according to their original 
    // position and CheckBox never 
    // loss his State when you 
    // Scroll the List Items. 
    return inView; 
} 

} 
+5

アダプタでgetCount()を呼び出すと、0が返されますか? – Gregory

+0

Grrr ...それです!私の質問は空ではないときに空に戻ります。穴を見つけてくれてありがとう:) – Martin

答えて

22

これはコメントで答えてしまったが、それは同様に本当の答えを得る可能性があります^^

getCount()は実際には0を返しているため、問題はクエリに空に戻ってきて、アダプタには戻っていません。

+0

ええ、私がコメントした後、私は自分自身に答えるのが好きでしたが、spam-meisterは私ができなかったと言っています:) – Martin

+0

申し訳ありませんが、私は解決策を複製するはずでした答えか、それをコメントに残してください。 – Gregory

+1

ありがとう、たくさんの男、どのように単純なエラーがすることができます;) – cV2

11

getViewが呼び出されない理由は他にもあります。私の場合、私はLinearLayout - TextViewListViewの2つの要素を持っていました。属性layout_heightTextViewに設定されていたため、fill_parentListView要素が画面外境界に設定されていました。 メソッドは、ListViewアイテムがユーザーに表示されるときにのみ呼び出されるため、呼び出されたことはありません。 layout_heightプロパティをwrap_contentに変更するとこの問題が解決しました。

+0

ありがとうございます。私はそれが既に画面の上部にあったいくつかの他の要素の上に固定されていたので、私のListViewは見えませんでした。 – Kacy

0

もう1つのことは、適切な引数を指定してsetContentView()を呼び出したことを確認することです。

関連する問題