我目前遇到了一个非常烦人的问题,我的应用程序:目标是从列表视图中读取不同的信息,这取决于用户点击的按钮。不幸的是,我的应用程序总是崩溃,我无法找到一个工作解决方案之间的类似问题(尝试更改数据库的版本,添加一些关于游标等)。Logcat:列_id不存在(SQL相关)
这里是我的代码的相关部分:
A. DatabaseAdapter类:
public class DatabaseAdapter {
public static final String DATABASE_TABLE = "Restaurants";
public static final String COL_CAT1 = "cat1";
public static final String KEY_ROWID = "_id";
public static final String COL_CAT2 = "cat2";
public static final String COL_NAME = "name";
public static final String COL_COMMENTS = "comments";
private Context myContext;
private SQLiteDatabase myDatabase;
private DatabaseHelper dbHelper;
private Cursor c;
// Constructor
public DatabaseAdapter(Context context) {
this.myContext = context;
}
public DatabaseAdapter open() throws SQLException {
dbHelper = new DatabaseHelper(myContext);
// TODO: or maybe?
// database = dbHelper.getWritableDatabase();
try {
dbHelper.createDatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myDatabase = dbHelper.getReadableDatabase();
return this;
}
public void close() {
if (c != null) {
c.close();
}
try {
dbHelper.close();
myDatabase.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Cursor findNameInTable(int myVariable) {
c = myDatabase
.query(DATABASE_TABLE, new String[] { COL_NAME , COL_COMMENTS },
COL_CAT1+"=?",
new String[] { Integer.toString(myVariable) }, null,
null, null);
return c;
}
}
B. ResultListViewActivity(结果都应该通过这个来显示在ListView中的活动):
public class ResultListViewActivity extends Activity {
private SimpleCursorAdapter cursorAdapter;
private DatabaseAdapter dbHelper;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_list_view);
dbHelper = new DatabaseAdapter(this); // open() method is in DatabaseAdapter
dbHelper.open();
displayListView();
}
private void displayListView() {
Bundle bundle = getIntent().getExtras();
int myVariable = bundle.getInt("myVariable");
Cursor c = dbHelper.findNameInTable(myVariable);
// the desired columns to be bound
String[] columns = new String[] { DatabaseAdapter.COL_NAME,
DatabaseAdapter.COL_COMMENTS, };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.name, R.id.comments, };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listview);
// Assign adapter to ListView
listView.setAdapter(cursorAdapter);
}
图片我的数据库的:正如你所看到的,实际上是一个_id列:
最后,我的logcat中的情况下画面:
如果你想看我的代码的其他部分,只是让我知道。预先感谢任何帮助!
参见[此帖](HTTP:/ /stackoverflow.com/a/14617726/1365960)。 – StarPinkER 2013-03-05 06:49:11