gpt4 book ai didi

java - 如何sqlite + listview + onListItemClick启动新的Activity?光标读取错误

转载 作者:行者123 更新时间:2023-12-02 00:12:32 25 4
gpt4 key购买 nike

借助这个tutorial我制作了一个有 3 个 Activity 的应用程序。在第一个 Activity (导入)中,我只是将一些值导入到 sqlite 数据库中。

这是我的 DatabaseHelper 类:

   public class DatabaseHelper_bp extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "bpDB";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table bp_import ( _id integer primary key, datetime text not null, systolic text not null, diastolic text not null, pulses text not null, notes text not null);";

public DatabaseHelper_bp(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}

// Method is called during an upgrade of the database,
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.w(DatabaseHelper_bp.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS bp_import");
onCreate(database);
}
}

以及我的 DAO 类用于我的度量/值:

    public class BpDAO {

private DatabaseHelper_bp dbHelper;

private SQLiteDatabase database;
/**
* Movie table related constants.
*/
public final static String bp_TABLE = "bp_import";
public final static String bp_ID = "_id";
public final static String bp_DT = "datetime";
public final static String bp_SYS = "systolic";
public final static String bp_DIA = "diastolic";
public final static String bp_PUL = "pulses";
public final static String bp_NOT = "notes";

/**
*
* @param context
*/
public BpDAO(Context context) {
dbHelper = new DatabaseHelper_bp(context);
database = dbHelper.getWritableDatabase();
}

/**
* \ Creates a new blood pressure measure
*
* @param datetime
* @param systolic
* @param diastolic
* @param pulses
* @param notes
* @return
*/
public long importBP(String datetime, String systolic, String diastolic,
String pulses, String notes) {
ContentValues values = new ContentValues();
values.put(bp_DT, datetime);
values.put(bp_SYS, systolic);
values.put(bp_DIA, diastolic);
values.put(bp_PUL, pulses);
values.put(bp_NOT, notes);
return database.insert(bp_TABLE, null, values);
}

/**
* Fetch all movies
*
* @return
*/
public Cursor fetchAll_bp() {
Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
bp_DIA, bp_DT, bp_ID }, null, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}

在第二个 Activity (历史记录)中,我有一个列表 Activity ,其中包含数据库每行的一些值。在这里一切正常。列表由数据库填充,一切正常 enter image description here

但是我的问题是当我尝试onListItemClick启动一个新的3个 Activity (详细信息)并将所选度量(ListItem)的所有值传递给它时, 应用程序强制关闭!

这是2个Activity(历史)的代码:

public class HistoryActivity extends ListActivity {

private BpDAO dao;

private SimpleCursorAdapter dbAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dao = new BpDAO(this);
Cursor bpList = dao.fetchAll_bp();
String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
R.id.bpDtHolder };
dbAdapter = new SimpleCursorAdapter(this, R.layout.history_bp, bpList,
from, target);
setListAdapter(dbAdapter);
}
//above is all good!
// try to make new activity on click - let's
// see...down here is the problem!
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, view, position, id);
Log.d("BPT", "Selected bp id =" + id);
// log says that i have selected an item with id : 11

Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);

String bp_DT = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_DT));
String bp_SYS = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_SYS));
String bp_DIA = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_DIA));
String bp_PUL = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_PUL));
String bp_NOT = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_NOT));

Log.d("BPT", "Selected bp details = { date=" + bp_DT + ", systolic="
+ bp_SYS + ", diastolic=" + bp_DIA + ", pulses=" + bp_PUL
+ ", notes=" + bp_NOT + " }");

Intent intent = new Intent(HistoryActivity.this, FromHistory.class);
intent.putExtra("bp_SYS", bp_SYS);
intent.putExtra("bp_DIA", bp_DIA);
intent.putExtra("bp_DT", bp_DT);
intent.putExtra("bp_PUL", bp_PUL);
intent.putExtra("bp_NOT", bp_NOT);
startActivity(intent);
}
// ----------------------------------------------------------------------------

}

这是日志猫:

16:50:48.513: D/BPT(562): Selected bp id =11
16:50:48.513: **E/CursorWindow(562): Failed to read row 1, column -1 from a CursorWindow which has 13 rows, 4 columns.**

最佳答案

在 BpDAO 类中的数据库查询方法 fetchAll_bp() 中,您似乎忘记在查询中包含注释 (bp_NOT)。因此怀疑它失败了,因为您的 onClickListener 正在游标中查找 bp_NOT,而它不包含该游标。

关于java - 如何sqlite + listview + onListItemClick启动新的Activity?光标读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12439574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com