在 ListView 中显示之前,将文本附加到从 SQLite 查询返回的数据
Append text to data returned from SQLite query before displaying in ListView
我正在从 SQLite 数据库中检索一组结果并将其显示在 ListView
中。
我想要做的是在结果值之前附加一些文本,然后再在实际 ListView
.
中显示它
例如,如果我从数据库中获取值 5
,我想在 ListView
中将其显示为 Value = 5
。
这是我用来更新 ListView
的代码:
private void displayListView() {
Cursor cursor = dbHelper.fetchAllSubjects();
String[] columns = new String[]{
getSubjectsDbAdapter.KEY_SUBJECT,
getSubjectsDbAdapter.KEY_SEMESTER,
getSubjectsDbAdapter.KEY_ATTENDED,
getSubjectsDbAdapter.KEY_TOTAL,
getSubjectsDbAdapter.KEY_PERCENT
};
int[] to = new int[]{
R.id.sub_out,
R.id.semester_out,
R.id.attended_out,
R.id.total_out,
R.id.percent_out
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, cursor, columns, to, 0);
subject_list.setAdapter(dataAdapter);
}
我曾尝试对 String[]
数组进行以下修改:
private void displayListView() {
Cursor cursor = dbHelper.fetchAllVals();
String[] columns = new String[]{
"Val1" + getSubjectsDbAdapter.KEY_1,
"Val2: " + getSubjectsDbAdapter.KEY_2,
"Val3: " + getSubjectsDbAdapter.KEY_3,
"Val4: " + getSubjectsDbAdapter.KEY_4,
"Val5: " + getSubjectsDbAdapter.KEY_5
};
int[] to = new int[]{
R.id.one,
R.id.two,
R.id.three,
R.id.four,
R.id.five
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, cursor, columns, to, 0);
subject_list.setAdapter(dataAdapter);
}
但这给了我以下错误:
java.lang.IllegalArgumentException: column 'Val2: val2Val3: val3' does not exist
我得到了答案here at SO。
Cursor allTaskcursor = databaseHelper.getAllTasks();
String[] from = {"name", "date"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_2, allTaskcursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == android.R.id.text2) {
int getIndex = cursor.getColumnIndex("date");
int date = cursor.getInt(getIndex);
TextView dateTextView = (TextView) view;
dateTextView.setText(date + " days");
return true;
}
return false;
}
});
allTaskListView.setAdapter(cursorAdapter);
我正在从 SQLite 数据库中检索一组结果并将其显示在 ListView
中。
我想要做的是在结果值之前附加一些文本,然后再在实际 ListView
.
中显示它
例如,如果我从数据库中获取值 5
,我想在 ListView
中将其显示为 Value = 5
。
这是我用来更新 ListView
的代码:
private void displayListView() {
Cursor cursor = dbHelper.fetchAllSubjects();
String[] columns = new String[]{
getSubjectsDbAdapter.KEY_SUBJECT,
getSubjectsDbAdapter.KEY_SEMESTER,
getSubjectsDbAdapter.KEY_ATTENDED,
getSubjectsDbAdapter.KEY_TOTAL,
getSubjectsDbAdapter.KEY_PERCENT
};
int[] to = new int[]{
R.id.sub_out,
R.id.semester_out,
R.id.attended_out,
R.id.total_out,
R.id.percent_out
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, cursor, columns, to, 0);
subject_list.setAdapter(dataAdapter);
}
我曾尝试对 String[]
数组进行以下修改:
private void displayListView() {
Cursor cursor = dbHelper.fetchAllVals();
String[] columns = new String[]{
"Val1" + getSubjectsDbAdapter.KEY_1,
"Val2: " + getSubjectsDbAdapter.KEY_2,
"Val3: " + getSubjectsDbAdapter.KEY_3,
"Val4: " + getSubjectsDbAdapter.KEY_4,
"Val5: " + getSubjectsDbAdapter.KEY_5
};
int[] to = new int[]{
R.id.one,
R.id.two,
R.id.three,
R.id.four,
R.id.five
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, cursor, columns, to, 0);
subject_list.setAdapter(dataAdapter);
}
但这给了我以下错误:
java.lang.IllegalArgumentException: column 'Val2: val2Val3: val3' does not exist
我得到了答案here at SO。
Cursor allTaskcursor = databaseHelper.getAllTasks();
String[] from = {"name", "date"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_2, allTaskcursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == android.R.id.text2) {
int getIndex = cursor.getColumnIndex("date");
int date = cursor.getInt(getIndex);
TextView dateTextView = (TextView) view;
dateTextView.setText(date + " days");
return true;
}
return false;
}
});
allTaskListView.setAdapter(cursorAdapter);