Java 类型不匹配:无法从列表转换<;类名>;指向光标:android错误

Java 类型不匹配:无法从列表转换<;类名>;指向光标:android错误,java,android,sqlite,Java,Android,Sqlite,我是android新手,正在学习将sqllite数据库连接到应用程序。这是在Cursor Cursor=(Cursor)db.getAllQuestions()中获取错误的主要活动行。 错误说明类型不匹配:无法从列表转换为光标。请有人帮我克服这个问题 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {

我是android新手,正在学习将sqllite数据库连接到应用程序。这是在
Cursor Cursor=(Cursor)db.getAllQuestions()中获取错误的主要活动行。
错误说明
类型不匹配:无法从列表转换为光标
。请有人帮我克服这个问题

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;
            }
公共列表getAllQuestions(){
List QuestionList=新建ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+表格\问题;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
addtodb question=newaddtodb();
setID(Integer.parseInt(cursor.getString(0));
setName(cursor.getString(1));
setPhoneNumber(cursor.getString(2));
//将联系人添加到列表中
问题列表。添加(问题);
}while(cursor.moveToNext());
}
//返回联系人列表
返回问题列表;
}

显然,您希望将长度为3的
列表包装成一个字符串。
您可以为此目的使用。例如:

List<?> resultList = getAllQuestion();
MatrixCursor cursor = new MatrixCursor(new String[] { "0", "1", "2" });
cursor.addRow(resultList);
List resultList=getAllQuestion();
MatrixCursor=新MatrixCursor(新字符串[]{“0”、“1”、“2”});
cursor.addRow(结果列表);

您需要返回游标对象而不是列表。这会有帮助。

发布
getAllQuestion()
方法如果
db.getAllQuestions()
返回一个
列表
,则无法将其转换为
光标
<代码>光标
db.query(…)返回
db.getAllQuestions()
需要返回光标