有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java类型不匹配:无法从列表<类名>转换为游标:安卓错误

我是安卓新手,正在学习将sqllite数据库连接到应用程序。这是在Cursor cursor = (Cursor) db.getAllQuestions();行中获取错误的主要活动。 错误是Type mismatch: cannot convert from List<class name> to Cursor。谁能帮我克服这个问题吗

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

共 (2) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    显然,你想把长度为3的List包装成一个Cursor。 为此,可以使用MatrixCursor。例如:

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