2014-02-26 19 views
1

我是新來的android和學習連接sqllite數據庫到應用程序。這是主要活動,並在Cursor cursor = (Cursor) db.getAllQuestions();行中出現錯誤。 錯誤說Type mismatch: cannot convert from List<class name> to Cursor。請有人幫我解決這個問題。類型不匹配:無法從列表<class name>轉換爲光標:android error

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

       try { 
     String destPath = "/data/data/" + getPackageName() 
       + "/databases/questions"; 
     File f = new File(destPath); 
     if (!f.exists()) { 
      CopyDB(getBaseContext().getAssets().open("questions"), 
        new FileOutputStream(destPath)); 
     } 
    } 

      catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Toast.makeText(MainActivity.this, "Error File", Toast.LENGTH_SHORT) 
       .show(); 
    } 
      catch (IOException e1) { 
     e1.printStackTrace(); 
     Toast.makeText(MainActivity.this, "Error IO", Toast.LENGTH_SHORT) 
       .show(); 
    } 



     DatabaseHandler db = new DatabaseHandler(this); 

     // db.open(); 
     long id = db.addQuestion(new addtodb(0, "Question1", "answer1")); 
     id = db.addQuestion(new addtodb(0, "Question2", "answer2")); 
     db.close(); 

     // db.open(); 
     Cursor cursor = (Cursor) db.getAllQuestions(); 
     if (cursor.moveToFirst()) 
     { 
      do { 
       DisplayRecord(cursor); 

      } 
      while(cursor.moveToNext()); 
     } 

     db.close(); 
    } 

     public void CopyDB(InputStream inputstream, OutputStream outputstream)   
       throws IOException { 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = inputstream.read(buffer))>0){ 
       outputstream.write(buffer, 0, length); 
      } 
      inputstream.close(); 
      outputstream.close(); 
     } 

     public void DisplayRecord(Cursor cursor){ 
      Toast.makeText(this, 
        "id:" + cursor.getString(0) + "\n" + 
        "Question:" + cursor.getString(1) + "\n" + 
        "Answer:" + cursor.getString(2), 
        Toast.LENGTH_SHORT).show(); 
     } 


      } 

這是我的getAllQuestions方法。

public List<addtodb> getAllQuestions() { 
       List<addtodb> QuestionList = new ArrayList<addtodb>(); 
       // Select All Query 
       String selectQuery = "SELECT * FROM " + TABLE_QUESTIONS; 

       SQLiteDatabase db = this.getWritableDatabase(); 
       Cursor cursor = db.rawQuery(selectQuery, null); 

       // looping through all rows and adding to list 
       if (cursor.moveToFirst()) { 
        do { 
         addtodb question = new addtodb(); 
         question.setID(Integer.parseInt(cursor.getString(0))); 
         question.setName(cursor.getString(1)); 
         question.setPhoneNumber(cursor.getString(2)); 
         // Adding contact to list 
         QuestionList.add(question); 
        } while (cursor.moveToNext()); 
       } 

       // return contact list 
       return QuestionList; 
      } 
+0

發佈'getAllQuestion()'方法 – Raghunandan

+1

如果'db.getAllQuestions()'返回一個'List',它就不能被轉換成'Cursor'。 '遊標'從'db.query(...)返回;' – thomasg

+0

'db.getAllQuestions()'需要返回一個遊標 –

回答

2

顯然要包裝你的List長度爲3成Cursor。 您可以使用MatrixCursor來達到此目的。例如:

List<?> resultList = getAllQuestion(); 
MatrixCursor cursor = new MatrixCursor(new String[] { "0", "1", "2" }); 
cursor.addRow(resultList); 
0

您需要返回遊標對象而不是List。這將有所幫助。

相關問題