//function which is in my DatabaseHelper class which extends SQLiteOpenHelper
public String getCoordinatesLatitude(int id) {
String rowLat = "";
SQLiteDatabase db = this.getReadableDatabase();
String latQuery = "SELECT " + KEY_LATITUDE + "FROM " + TABLE_COORDINATES + "WHERE " + KEY_ID + "=" + id;
Cursor cursorr = db.rawQuery(latQuery,null);
if (cursorr != null){
cursorr.moveToPosition(id);
rowLat = cursorr.getString(cursorr.getColumnIndex(KEY_LATITUDE));
}
// return coordinates
return rowLat;
}
//function in my main activity
public void onListItemClick(ListView l, View v, int position, long id) {
selectedFromList = (String) (l.getItemAtPosition(position));
selectedItem = (int) l.getItemIdAtPosition(position);
String rowLat = helper.getCoordinatesLatitude(selectedItem);
Toast.makeText(getApplicationContext(), rowLat, Toast.LENGTH_SHORT).show();
}
我有listview
,其中包含locations
。每个位置都包含latitude
,longitude
和日期。我基本上想要的是点击listview
项,并根据其latitude
ID从我的数据库中检索项listview
值。我设置了getCoordinatesLatitude()
函数,但我不知道我的代码是否正确,因为当我点击listview
项时,我强行关闭,logcat显示nullPointerException
。我怎样才能做到这一点?谢谢
我的列表视图是什么样的
UPDATE !!!! 我设法摆脱了nullPointerException错误,并将我的代码更改为此
public String getLatitudeFromId(long id) {
String rowLat = "not found";
SQLiteDatabase db = this.getReadableDatabase();
//String latQuery = "SELECT " + KEY_LATITUDE + " FROM " + TABLE_COORDINATES + " WHERE " + KEY_ID + "=" + id;
//Cursor cursor = db.rawQuery(latQuery,null);
Cursor cursor = db.query(TABLE_COORDINATES, new String[] { "latitude" },"id="+id, null, null, null,null);
if (cursor.moveToFirst()){
cursor.moveToPosition((int) id);
rowLat = cursor.getString(cursor.getColumnIndex("latitude"));
}
cursor.close();
db.close();
// return coordinates
return rowLat;
}
和
public void onListItemClick(ListView l, View v, int position, long id) {
DatabaseHelper helper = new DatabaseHelper(this);
selectedFromList = (String) (l.getItemAtPosition(position));
selectedItem = l.getItemIdAtPosition(position);
String rowLat = helper.getLatitudeFromId(selectedItem);
Toast.makeText(getApplicationContext(), rowLat, Toast.LENGTH_SHORT).show();
}
但现在,当我点击listview项目时,找不到toast,因此它会在if(cursor.moveToFirst())语句中停止,因为游标为空。当我的listview充满项目时,如何将光标置空? :P
UPDATE2 我只是通过将查询更改为此Cursor游标= db.query(TABLE_COORDINATES,new String [] {KEY_DATE},null,null,null,null,null)来修复问题;其中KEY_DATE是您在列表视图中单击项目时要显示的列名
答案 0 :(得分:1)
如果已分配给listview适配器的数组是“array”,则可以检索array.get(position).getLatitude(); (我假设您使用了自定义适配器)
答案 1 :(得分:0)
首先,您的表必须将列设置为名为_id而不是id的主键。然后你可以传递给getLatitudeFromId你从onItemClick获得的id。
将getLatitudeFromId(long id)更改为:
public String getLatitudeFromId(long id) {
String rowLat = "not found";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_COORDINATES, new String[] { "latitude" },"_id=" + id, null, null, null,null);
if (cursor.moveToFirst()){
rowLat = cursor.getString(cursor.getColumnIndex("latitude"));
}
cursor.close();
db.close();
// return coordinates
return rowLat;
}
将onListItemClick更改为:
public void onListItemClick(ListView l, View v, int position, long id) {
DatabaseHelper helper = new DatabaseHelper(this);
// ??? selectedFromList = (String) (l.getItemAtPosition(position));
// ??? selectedItem = l.getItemIdAtPosition(position);
String rowLat = helper.getLatitudeFromId(id);
Toast.makeText(getApplicationContext(), rowLat, Toast.LENGTH_SHORT).show();
}
注意:l.getItemAtPosition(position)返回的视图不是字符串,而不是int
答案 2 :(得分:0)
我实际上已经解决了问题。奇怪的是,我改变了我的查询 游标cursor = db.query(TABLE_COORDINATES,new String [] {KEY_DATE},null,null,null,null,null); 只是让选择为null并且只有我想要的列,并且它有效!这个问题可以标记为已回答。