rawQuery - CursorAdapter

时间:2011-05-10 01:33:40

标签: android

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

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);



}

}

1 个答案:

答案 0 :(得分:2)

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

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

请在此处查看我的回答和解释column '_id' does not exist problem