getView没有得到叫什么名字? [英] getView not getting called?
问题描述
我有延伸simplecursoradapter的适配器。新的观点是应该采取从数据库游标与图像一起,填充列表与一对夫妇的复选框。出于某种原因,我似乎无法看到,我的getView甚至没有被调用。我有内部getView一个断点,它永远不会到达那里,并只需在列表中显示了空。
任何人都可以看看直通,看看我做错了。
公共类TakeStudentAttendance扩展ListActivity {
私人gradeBookDbAdapter mDbHelper;
私人长期mRowId;
私人TextView的mNameText;
私人字符串类名;
私人布尔new_attendance = FALSE;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
光标螺栓; mDbHelper =新gradeBookDbAdapter(本);
mDbHelper.open();
mRowId =(savedInstanceState == NULL)?空值
(龙)savedInstanceState
.getSerializable(gradeBookDbAdapter.KEY_ROWID);
如果(mRowId == NULL){
捆绑额外= getIntent()getExtras()。
mRowId =临时演员!= NULL?演员
.getLong(gradeBookDbAdapter.KEY_ROWID):空;
}
//拉类数据
螺柱= mDbHelper.fetchClass(mRowId);
startManagingCursor(螺栓); 类名= stud.getString(
stud.getColumnIndexOrThrow(gradeBookDbAdapter.KEY_CLASSNAME));
字符串title =考勤+类名;
的setTitle(职称); 的setContentView(R.layout.attendance_list);
按钮doneButton =(按钮)findViewById(R.id.Done);
doneButton.setOnClickListener(mAttendanceActivity); //检查previous起止日期
字符串prevdate = stud.getString(
stud.getColumnIndexOrThrow(gradeBookDbAdapter.KEY_ preVDATE)); 螺柱= mDbHelper.fetchAttendanceByClass(mRowId); //这个查询产量_id,姓名,
//出席,迟到,DTIME 如果(mDbHelper.getClassDate()== prevdate){
// previous日期是一样的,所以我们又来考勤:保留值
new_attendance = FALSE;
}
其他{
//日期是不同的,所以我们从头开始,所有学生都
//缺席,直到数present。我只需要姓名和将填充出席
new_attendance = TRUE;
//在出席启动,没有一个是present。坐落在数据库中的所有条目不present(0)
setNoAttend(螺栓,mRowId);
//重新设置光标位置
stud.moveToFirst();
} //创建一个数组来指定,我们要在列表中显示的字段
的String [] =由新的String [] {gradeBookDbAdapter.KEY_NAME,
gradeBookDbAdapter.KEY_ROWID,
gradeBookDbAdapter.KEY_ATTEND,
gradeBookDbAdapter.KEY_LATE,
gradeBookDbAdapter.KEY_DTIME}; //我们希望这些字段绑定到域的阵列(在此情况下,仅有文本1)
INT []为= INT新[] {R.id.stuname,
R.id.stuIndex,
R.id.attend,
R.id.late,
R.id.stuIndex}; //现在创建一个简单的游标适配器,并将其设置为显示
// mRowId持有类指数。
MyDataAdapter螺栓=
新MyDataAdapter(这一点,R.layout.show_attendance,螺柱,发件人,收件人,mRowId,new_attendance);
setListAdapter(螺栓);
}
下面是我的适配器code:
公共类MyDataAdapter扩展SimpleCursorAdapter {
私人光标C;
私人上下文的背景下;
私人长期classnum;
私人gradeBookDbAdapter mDbHelper;
私人布尔newValues;
私人的ArrayList<串GT;名单=新的ArrayList<串GT;();
私人的ArrayList<布尔> itemCheckedHere =新的ArrayList<布尔>();
私人的ArrayList<布尔> itemCheckedLate =新的ArrayList<布尔>();
私人的ArrayList<整数GT; itemCheckedIdx =新的ArrayList<整数GT;();
INT idxCol;
INT IDX;// itemChecked将存储选中项的位置。公共MyDataAdapter(上下文的背景下,INT布局,光标C,的String []从,
INT []以龙mRowId,布尔new_attendance){
超(背景下,布局,C,从,到);
this.c = C;
this.context =背景;
mDbHelper =新gradeBookDbAdapter(背景);
mDbHelper.open();
classnum = mRowId;
newValues = new_attendance;
c.moveToFirst();
的for(int i = 0; I< c.getCount();我++){
itemCheckedHere.add(I,FALSE); //初始化所有项目进行假值
itemCheckedLate.add(I,FALSE); //初始化所有项目进行假值
}
}公共查看getView(最终诠释POS,查看inView,父母的ViewGroup){
档案文件;
ImageView的studentPhoto; 如果(inView == NULL){
LayoutInflater吹气=(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inView = inflater.inflate(R.layout.show_attendance,NULL);
} //设置名称字段
最终的TextView studentName =(TextView中)inView.findViewById(R.id.stuname);
最终的TextView studentIndex =(TextView中)inView.findViewById(R.id.stuIndex);
如果(studentName!= NULL)
{
c.moveToPosition(POS)
INT指数= c.getColumnIndex(gradeBookDbAdapter.KEY_NAME);
字符串名称= c.getString(指数);
studentName.setText(名); 指数= c.getColumnIndex(gradeBookDbAdapter.KEY_STUDENT);
字符串指数= c.getString(指数);
studentIndex.setText(索引);
//设置照片图标 文件=新的文件(Environment.getExternalStorageDirectory()+
/成绩册/+姓名+.JPG);
studentPhoto =(ImageView的)inView.findViewById(R.id.icon); 如果(file.exists()){
字符串文件名= file.getAbsolutePath();
BitmapFactory.Options选择采用=新BitmapFactory.Options();
位图BM; BM = BitmapFactory.de codeFILE(文件名,选择采用);
studentPhoto.setImageBitmap(BM);
}
其他{
//使用图标图像
studentPhoto.setImageResource(R.drawable.person_icon);
} }
最后复选框cBoxH =(复选框)inView.findViewById(R.id.attend);
最后复选框cBoxL =(复选框)inView.findViewById(R.id.late);
cBoxH.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ 复选框CB =(复选框)v.findViewById(R.id.attend); 如果(cb.isChecked()){
itemCheckedHere.set(POS,真);
INT指数=新的整数(studentIndex.getText()的toString());
mDbHelper.insertAttend(索引,classnum,1);
}否则如果(!cb.isChecked()){
itemCheckedHere.set(POS,FALSE);
INT指数=新的整数(studentIndex.getText()的toString());
mDbHelper.deleteAttend(索引,classnum);
}
}
});
cBoxL.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){
复选框CB =(复选框)v.findViewById(R.id.late); 如果(cb.isChecked()){
itemCheckedLate.set(POS,真);
//做一些操作在这里
}否则如果(!cb.isChecked()){
itemCheckedLate.set(POS,FALSE);
//做一些操作在这里
}
}
});
cBoxH.setChecked(itemCheckedHere.get(POS)); //这将选中或取消
cBoxL.setChecked(itemCheckedLate.get(POS)); //这将选中或取消
//复选框的ListView
//按其原来
//位置和CheckBox始终是不可
//损失他国时,
//滚动列表项。
返回inView;
}}
这得到了回答的评论,但它也可能得到一个真正的答案^^
getCount将()实际上是返回0,所以这个问题是在查询回来空的,而不是在适配器。
I have an adapter that extends simplecursoradapter. The new view is supposed to take a cursor from a database along with an image, populate a list with a couple of checkboxes. For some reason I can't seem to see, my getView is not even being called. I have a breakpoint inside getView and it never gets there and the list just shows up empty. Can anyone take a look thru and see what I've done wrong
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);
}
Here's my adapter code:
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;
}
}
This got answered in the comment, but it might as well get a real answer ^^
getCount() is actually returning 0, so the problem is in the query coming back empty, and not in the adapter.
这篇关于getView没有得到叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!