作为标题,我试图从SQLite数据库中获取值,点击ListView的Item。此ListView以DESC顺序显示数据库的每个值。单击它后,对话框应该显示该值的摘要。
这是onItemClick方法:
Val_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = position + 1;
String[] projection2 = {
_ID,
DiabeticContract.DiabeticEntry.COLUMN_GLYCAEMIA_VALUES,
DiabeticContract.DiabeticEntry.COLUMN_DATES,
};
Cursor cursor2 = db2.query(
DiabeticContract.DiabeticEntry.TABLE_NAME, // The table to query
projection2, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // Don't group the rows
null, // Don't filter by row groups
null); // The sort order
cursor2.move(pos);
String date, notes;
int g_val = 0;
g_val = cursor2.getInt(cursor2.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_GLYCAEMIA_VALUES));
date = cursor2.getString(cursor2.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_DATES));
//notes = cursor2.getString(cursor2.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_NOTES));
customDialog(DIALOG_TITLE, date, "");
cursor2.close();
}
});
问题是,如果我点击列表视图中的第一个项目,该位置等于1,它将获取数据库第一行中的值,而不是最后一行。
我该如何解决?
非常感谢!
编辑:
public class DiabeticCursorAdapter extends CursorAdapter
{
private ImageView check;
public int glyc_value;
public String glyc_date;
public DiabeticCursorAdapter(Context context, Cursor c) {
super(context, c,0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.gly_item_list, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView glycaemia_value = (TextView) view.findViewById(R.id.Glyc_value);
TextView test_date = (TextView) view.findViewById (R.id.Date_time);
check = (ImageView) view.findViewById(R.id.check_view);
glyc_value = cursor.getInt(cursor.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_GLYCAEMIA_VALUES));
glyc_date = cursor.getString(cursor.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_DATES));
checkVal(glyc_value);
glycaemia_value.setText("Glycaemia Value : " + glyc_value + " mg/dl");
test_date.setText("Added : " + glyc_date);
}
private void checkVal(int n)
{
if (n< 75 || n >= 125)
{
check.setImageResource(R.drawable.check_red);
} else
if (n >= 75 && n <100)
{
check.setImageResource(R.drawable.check_green);
} else
if (n >= 100 && n < 125)
{
check.setImageResource(R.drawable.check_yellow);
}
}
}
编辑2:列表视图方法上的显示
private void DisplayDataValues() {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {
_ID,
DiabeticContract.DiabeticEntry.COLUMN_GLYCAEMIA_VALUES,
DiabeticContract.DiabeticEntry.COLUMN_DATES,
DiabeticContract.DiabeticEntry.COLUMN_NOTES,
};
// Perform a query on the pets table
Cursor curs = db.query(
DiabeticContract.DiabeticEntry.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // Don't group the rows
null, // Don't filter by row groups
_ID + " DESC"); // The sort order
DiabeticCursorAdapter adapter = new DiabeticCursorAdapter(this, curs);
Val_List.setAdapter(adapter);
}
编辑3:这是解决方案:(感谢@Mike M。)
Val_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = position;
curs.moveToPosition(pos);
String date, notes;
int g_val;
g_val = curs.getInt(curs.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_GLYCAEMIA_VALUES));
date = curs.getString(curs.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_DATES));
notes = curs.getString(curs.getColumnIndexOrThrow(DiabeticContract.DiabeticEntry.COLUMN_NOTES));
customDialog(DIALOG_TITLE, date, g_val, notes, "");
}
});