- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个扩展 simplecursoradapter 的适配器。新 View 应该从数据库中获取光标和图像,用几个复选框填充列表。出于某种原因,我似乎看不到,我的 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;
}
}
最佳答案
这在评论中得到了回答,但它还不如得到一个真正的答案^^
getCount() 实际上返回 0,所以问题出在返回空的查询中,而不是适配器中。
关于android - getView 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6633593/
我想要自定义微调器,在下拉列表中包含图像和 TextView ,因此我创建了不同的布局并对其进行了膨胀,其工作正常,但对于按微调器之前的微调器布局,我只想有一个类似“选择”的文本来自列表”但是当我用
所以我正在开发实时 ListView ,我需要 ListView 在单击按钮时更新,这些也是如此: private void ReloadData(){ DataList.clear();
我知道其他人也问过这个问题,但我看过其他解决方案,但仍然无法使其发挥作用。 适配器代码: private class CustomTextAndImageAdapter extends ArrayAd
我有一个通过 asynctask 填充的自定义 ListView 适配器,我在 onprogress 函数中调用 notificationdatasetchanged,并且 getCount() 返回
我正在显示联系人列表。一切都很好,除了设备有 1620 个联系人,所以列表滚动非常慢,甚至有时会挂起。 请帮帮我。 我尝试使用检查 ConvertView!=null 的 getView 方法,但它总
我想在每张图片下方添加一个文本,这是我的实际代码: @Override public View getView(int position, View convertView, ViewGroup pa
我在 Adapter extensions override of getView 中经常看到这一点: @Override public View getView(int position,
我正在尝试在对话框中制作 ListView 。但是我的适配器中的 getView 函数永远不会被调用。有什么想法吗? 适配器: public class DialogAdapter extends B
我有 public class MainActivity extends FragmentActivity 我编写了实现 AsyncResponse 的 Loader 类: public class
我有这样一段代码:)我想编辑我的自定义首选项布局的文本属性。但是从 getView 函数对对象所做的任何更改都不会影响首选项屏幕中的实际列表。有任何想法吗?我知道我不能扩展 PreferenceScr
我有一个扩展 simplecursoradapter 的适配器。新 View 应该从数据库中获取光标和图像,用几个复选框填充列表。出于某种原因,我似乎看不到,我的 getView 甚至没有被调用。我在
谁能解释一下这两种 getView() 实现之间的区别是什么? 第一个简单地检查 convertView 是否为 null;如果它为 null,则膨胀一个新的 View 对象。然后给子View设置合适
我做了一个 SingleItemAdapter extends ArrayAdapter,这个定制的 Adapter 用于 ListView。 在这个 SingleItemAdapter 中,我做了一
这里是相当新的 Android 开发者。 我遇到了一个奇怪的问题,我不确定如何解决。我在这里阅读了很多问题,听起来它们与我遇到的问题相同,但他们问题的解决方案似乎永远不适用于这里发生的事情。 我使用扩
我有一个带有 ListFragment 的自定义适配器,但根本没有调用适配器 getView()。 这是适配器的样子 - public class ModuleListItemAdapter exte
我正在将自定义 SimpleCursorAdapter 设置为 ListView。出于某种原因,对于数据库中的每个项目,FriendAdapter 的 getView() 被调用两次。经过一些调查(我
我有一个包含自定义适配器和自定义行的列表。 问题是当我滚动 ListView 时,图像位于错误的位置... 这是自定义行 xml: 这是 GetView: List it
public View getView(int index, View view, ViewGroup parent){ if (view == null) { // for the firs
我添加了两个按钮(添加和删除)来控制 ListView ,当我删除一个项目时,该项目不会立即在 ListView 中消失,只有当我在 ListView 上滑动时,新项目才会消失。除非我触摸屏幕或在 L
我已经多次尝试在我的 Controller 上使用 this.getView(),但我总是在我的控制台上收到 getView is not a function 错误。 是否有任何“技巧”或我可以做的
我是一名优秀的程序员,十分优秀!