2011-07-05 127 views
0

好吧,所以我有一个游标适配器,我从各种来源拼凑起来,大部分似乎都起作用 - 除了我的名字字段。我有两个在每个名称的列表视图上的复选框,但我的名字都出来了(名字)我对TextViews做了什么?

任何人发现我创建的问题?您的帮助将不胜感激。

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

你会踢你自己。您忘记将光标移至与ListView中索引对应的位置:

c.moveToPosition(pos) 
+0

哎呀!谢谢。我希望这很简单,但不是那么简单,我看起来像一个总数n00b。一定很累。谢谢菲利普! – Martin

1

适配器中的光标未设置为正确的行。

首先,您应该摆脱游标和上下文自己的成员变量!然后尝试实施的CursorAdapter的bindView方法,它会提供已经被设置为当前行光标:

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 

}