Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在Android中读取数据库信息_Java_Android_Database_Eclipse_Sqlite - Fatal编程技术网

Java 无法在Android中读取数据库信息

Java 无法在Android中读取数据库信息,java,android,database,eclipse,sqlite,Java,Android,Database,Eclipse,Sqlite,我正在尝试从SQL数据库读取数据。我认为我的代码是正确的,但有些代码无法显示在文本框中。当我使用compareData函数时,应用程序刚刚关闭 我有不同的闹钟。我想阅读所有这些文件,并显示它们的id和名称 这是我的密码: public class RingAlarm extends Activity { TextView texto; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto

我正在尝试从SQL数据库读取数据。我认为我的代码是正确的,但有些代码无法显示在文本框中。当我使用compareData函数时,应用程序刚刚关闭

我有不同的闹钟。我想阅读所有这些文件,并显示它们的id和名称

这是我的密码:

public class RingAlarm extends Activity {

TextView texto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ring_alarm);

    texto = (TextView) findViewById(R.id.textView1);

    texto.setText(compareData());
}

public String compareData(){
    String result = "";
    String str1 = "";
    String str2 = "";

    String[] columns = new String[]{Database.COLUMN_ALARM_ID, Database.COLUMN_ALARM_NAME};

    Cursor cursor = Database.getDatabase().query(Database.ALARM_TABLE, columns, null, null, null, null, null);

    while(cursor.moveToNext()){
        str1 = str1 + cursor.getString(0);
        str2 = str2 + cursor.getString(1);
        result = result + str1 + " - " + str2 + "\n";
    }

    cursor.close();

    return result;
}
}

您不应该在主UI线程上执行数据库I/O,因为这可能会导致应用程序挂起和崩溃

使用异步任务是最简单的解决方案。AsyncTask允许您在后台执行较长时间运行的任务,例如查询数据库或网络I/O,而不会中断UI

以下代码将compareData方法合并到AsyncTask的doInBackground方法中。然后,可以使用doInBackground返回的结果在onPostExecute方法中设置TextView。所有计算都应该在doInBackground中完成,UI对象应该在onPostExecute中设置。要在onCreate中执行此任务,只需实例化该任务并调用.execute

public class RingAlarm extends Activity {

    TextView texto;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ring_alarm);

        MyTask myTask = new MyTask();
        myTask.execute();
    }

    private class MyTask extends AsyncTask<Void, Void, String> {

        /**
         * The system calls this to perform work in a worker thread and
         * delivers it the parameters given to AsyncTask.execute()
         */
        @Override
        protected String doInBackground(Void... params) {
            String result = "";
            String str1 = "";
            String str2 = "";

            String[] columns = new String[]{Database.COLUMN_ALARM_ID, Database.COLUMN_ALARM_NAME};

            Cursor cursor = Database.getDatabase().query(Database.ALARM_TABLE, columns, null, null, null, null, null);

            while(cursor.moveToNext()){
                str1 = str1 + cursor.getString(0);
                str2 = str2 + cursor.getString(1);
                result = result + str1 + " - " + str2 + "\n";
            }

            cursor.close();

            return result;
        }

        /**
        * The system calls this to perform work in the UI thread and delivers
        * the result from doInBackground()
        */
        @Override
        protected void onPostExecute(String result) {
            texto = (TextView) findViewById(R.id.textView1);

            texto.setText(result);    
        }
    }

}
此链接全面解释了AsyncTask的工作原理:


希望这有帮助

试着打印一些日志。你在结果中得到了什么。请从logcat发布异常stacktrace