【问题标题】:rawQuery - CursorAdapterrawQuery - 游标适配器
【发布时间】:2011-05-10 01:33:40
【问题描述】:

进入此活动时,应用程序被强制关闭。我实际上注意到,如果我删除光标部分,活动不会崩溃。帮助将不胜感激。

public class SearchResults extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchresults);



    Database myDbHelper = new Database(null);
    myDbHelper = new Database(this);

    try {
        myDbHelper.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");
        }

try {



}catch(SQLException sqle){

    throw sqle;

  }



    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    String query = intent.getStringExtra(SearchManager.QUERY);


    SQLiteDatabase myDb = myDbHelper.getReadableDatabase();
    String q = "SELECT BookTitle, _ISBN FROM Books WHERE BookTitle LIKE" + query;
    Cursor c = myDb.rawQuery(q, null);
    startManagingCursor(c);



 // the desired columns to be bound
    String[] columns = new String[] { "Books._ISBN", "Books.BookTitle" }; 
 // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.ISBN_entry, R.id.Title_entry };

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.listlayout, c, columns, to);
    this.setListAdapter(mAdapter);



}

}

【问题讨论】:

    标签: android


    【解决方案1】:

    光标需要一个名为“_id”的列 - 将您的查询更改为 ISBN 列的别名,如下所示...

    String q = "SELECT _ISBN as _id, BookTile FROM Books WHERE BookTitle LIKE" + query;
    

    在这里查看我的回答和解释column '_id' does not exist problem

    【讨论】:

    • True 没有注意到这一点,但在此之前它仍然是强制应用程序强制关闭的其他因素。我从一些实验中得出的结论是,在某些地方我以可读的方式打开数据库并尝试让我的光标通过查询。
    • 问题出在其他地方,这解决了我的问题,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2017-03-03
    • 1970-01-01
    相关资源
    最近更新 更多