如何在doINBackground android中设置textview

时间:2016-11-11 09:58:17

标签: java android android-runonuithread

我想访问postgresql数据库,但我遇到AsyncTaskdoInBackground命令的问题。我不知道为什么我无法在onPostExecute更新我的textview。任何人都可以解释我的问题是什么?这是我的代码:

package com.example.armyhealthcare;

import java.sql.*;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;


public class MainActivity extends Activity {
    private static final String url = "jdbc:postgresql://elmer-02.db.elephantsql.com.5432/xyz";
    private static final String user = "user";
    private static final String pass = "pass";
    private TextView firstname, lastname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        firstname = (TextView) findViewById(R.id.textfname);
        lastname = (TextView) findViewById(R.id.textlname);
        Button buttonload = (Button) findViewById(R.id.buttonload);

        buttonload.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                new mytask().execute();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private class mytask extends AsyncTask<Void, Void, Void>{
        private String finame=""; 
        private String laname="";
        @Override
        protected Void doInBackground(Void... arg0){

            try{
                Class.forName("org.postgresql.Driver");
                Connection con = DriverManager.getConnection(url, user, pass);

                    Statement st = con.createStatement();                   
                    final ResultSet rs = st.executeQuery("select * from book");
                    rs.next();
                    finame= rs.getString(1);
                    laname= "Show Me";

            }
            catch(Exception e){
                e.printStackTrace();

            }
            return null;

        }

        @Override
        protected void onPostExecute(Void result){
            firstname.setText("finame");
            lastname.setText(laname);
            super.onPostExecute(result);
        }

    }

}

3 个答案:

答案 0 :(得分:2)

要在doInBackground中设置文本,您可以使用它。

runOnUiThread(new Runnable() {
               @Override
               public void run() {
                  txtView.setText("Message");       
               }
            }); 

试试这个:

Updating TextView from Async Task which use custom program dialog

答案 1 :(得分:0)

您可以使用runOnUiThead()或在onPostExexute()

中设置文字

示例:

runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            tvText.setText("ABC");
                        }
                    });

已编辑: 好方法:

private class mytask extends AsyncTask<Void, Void, Void> {
        private String finame="";
        private String laname="";
        @Override
        protected Void doInBackground(Void... arg0){

            try{
                Class.forName("org.postgresql.Driver");
                Connection con = DriverManager.getConnection(url, user, pass);

                Statement st = con.createStatement();
                final ResultSet rs = st.executeQuery("select * from book");
                rs.next();
                finame= rs.getString(1);
                laname= "Show Me";
            }
            catch(Exception e){
                e.printStackTrace();

            }
            return null;

        }

        @Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);
            firstname.setText(finame);
            lastname.setText(laname);
        }

    }

糟糕的方式:

private class mytask extends AsyncTask<Void, Void, Void> {
        private String finame="";
        private String laname="";
        @Override
        protected Void doInBackground(Void... arg0){

            try{
                Class.forName("org.postgresql.Driver");
                Connection con = DriverManager.getConnection(url, user, pass);

                Statement st = con.createStatement();
                final ResultSet rs = st.executeQuery("select * from book");
                rs.next();
                finame= rs.getString(1);
                laname= "Show Me";

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        firstname.setText(finame);
                        lastname.setText(laname);
                    }
                });

            }
            catch(Exception e){
                e.printStackTrace();

            }
            return null;

        }

        @Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);
        }

    }

设置这样的名字和姓氏

答案 2 :(得分:0)

将结果或对象从String doInBackground()传递到onPostExecute(String result)

请按照示例进行操作。

来自doInBackground()方法:

protected String doInBackground(Void... arg0){

         //Your code here
         return rs.getString(1);;
}

来自onPostExecute()方法:

@Override
protected void onPostExecute(String result){
     super.onPostExecute(result);
     firstname.setText(result);
     lastname.setText("Show Me");

}