0

データベースエントリ全体を処理する非同期タスクを作成し、それ自体に追加される te​​xtView でエントリが作成されたことをユーザーに知らせようとしています。内のビューに触れることができないことは理解していますがdoInBackground、他の方法を機能させることはできません。私のコードを AsyncTask 内で動作させる方法を誰かに説明してもらえますか?

コード:

private class DBADDITION extends AsyncTask<Object, Void, Object> {

        @Override
        protected String doInBackground(Object... params) {
            DBAdapter my_database = new DBAdapter(getApplicationContext());
            logout.append("\n" + "Start" + " ");
            my_database.open();
            String temp = input.getText().toString();

            int i = Integer.parseInt(temp);
            for (int j = 1; j <= i; j++) {
                db.createEntry("example", 10 + j);
                logout.setText("\n" + j + logout.getText());

            }
            db.close();
            return "it worked";
        }

        protected void onProgressUpdate(Integer... progress) {

        }

    }
4

3 に答える 3

0

次のように、Activity.runOnUIThread() を使用して setText を設定します。

private class DBADDITION extends AsyncTask<Object, Void, Object> {

    @Override
    protected String doInBackground(Object... params) {
        DBAdapter my_database = new DBAdapter(getApplicationContext());
        logout.append("\n" + "Start" + " ");
        my_database.open();


        final String temp = input.getText().toString();
        int i = Integer.parseInt(temp);
        for (int j = 1; j <= i; j++) {
            db.createEntry("example", 10 + j);
            youractivity.this.runOnUiThread(new Runnable() {
                public void run() {
                     logout.setText("\n" + j + logout.getText());
                }
        );

        }
        db.close();
        return "it worked";
    }

    protected void onProgressUpdate(Integer... progress) {

    }

}
于 2013-05-07T07:13:46.733 に答える