2011-05-10 40 views
0

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

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



} 

}

回答

2

光标需要一个所谓的 '_id' 列 - 改变你的查询到你的别名ISBN列如下...

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

见我的答案和解释这里column '_id' does not exist problem

+0

真的没有注意到,但它仍然是其他东西,迫使应用程序强制关闭之前。我从一些实验中得出的结论是,有些东西是我打开数据库的可读性,并试图让我的光标通过查询。 – GGe 2011-05-10 01:49:14

+0

问题在别处,这解决了我的问题,谢谢! – GGe 2011-05-10 05:39:49