2011-07-05 5 views
0

OKだから私はさまざまなソースから一緒に手錠をかけたカーソルアダプタを持っていて、主に自分の名前フィールドを除いて動作するようです。各名前のリストビューに2つのチェックボックスがありますが、私の名前はすべて同じ名前(同じ名前)になります私はTextViewに何をしましたか?

誰かが私が作成した問題を見つけますか?あなたの助けに感謝します。

public class MyDataAdapter extends SimpleCursorAdapter { 
private Cursor c; 
private Context context; 
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) { 
    super(context, layout, c, from, to); 
    this.c = c; 
    this.context = context; 
    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) { 
    TextView studentName; 
    ImageView studentPhoto; 

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

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

    // set up name field 

    studentName = (TextView) inView.findViewById(R.id.stuname); 
    if (studentName != null) 
    { 
      int index = c.getColumnIndex(gradeBookDbAdapter.KEY_NAME); 
      String name = c.getString(index); 
      studentName.setText(name); 
    } 




    cBoxH.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

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

      if (cb.isChecked()) { 
       itemCheckedHere.set(pos, true); 
       // do some operations here 
      } else if (!cb.isChecked()) { 
       itemCheckedHere.set(pos, false); 
       // do some operations here 
      } 
     } 
    }); 
    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; 
} 

答えて

0

あなたは自分を蹴るつもりです。あなたは、リストビュー内のインデックスに対応する位置にカーソルを移動するのを忘れ:

c.moveToPosition(pos) 
+0

なんてこった:次にCursorAdapterのbindViewメソッドを実装しようと、それはすでに現在の行にカーソルセットを提供します!ありがとう。私はそれがシンプルだと思っていたが、それほどシンプルではないが、私は合計n00bのように見える。疲れているはずです。ありがとうフィリップ! – Martin

1

アダプタでカーソルを正しい行に設定されていません。

まず、カーソルとコンテキストのメンバー変数を取り除く必要があります。

public class MyDataAdapter extends SimpleCursorAdapter { 
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>(); 


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

public MyDataAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 
    super(context, layout, c, from, to); 

    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 void bindView (View inView, Context context, Cursor cursor){ 
    super.bindView(inView,context,cursor); 

    TextView studentName; 
    ImageView studentPhoto; 

    //inView is never null in bindView 

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

    // set up name field 

    studentName = (TextView) inView.findViewById(R.id.stuname); 
    if (studentName != null) 
    { 
      int index = cursor.getColumnIndex(gradeBookDbAdapter.KEY_NAME); 
      String name = cursor.getString(index); 
      studentName.setText(name); 
    } 

    //your other code 
} 

//other methods 

}

関連する問題