【问题标题】:how to set result of BackgroundWorker to TextView如何将 BackgroundWorker 的结果设置为 TextView
【发布时间】:2017-06-28 21:44:48
【问题描述】:

我对 android 非常陌生,无法将数据库中的结果输入屏幕上的TextView。目前,我的应用程序在单击按钮后返回一条消息。我在屏幕上有两个TextView 元素txtViewBegintxtViewEind。从mySql(通过php)我得到一个像22-02-2014 24-05-2017 这样的字符串,我将其拆分为Strings (.split(" ")) 的数组。

我可以使用后台工作人员的setTextView(onPostExecute 吗?)

或者我应该在我的MainActivity 中这样做吗?在这方面仍然是菜鸟。对不起。

这是我的BackgroundWorker

public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx){
    context = ctx;
}


@Override
protected String doInBackground(String... params) {
    String type = params[0];
    String login_url= "http://*************/****.php";
    if(type.equals("Draai")){
        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(false);
            httpURLConnection.setDoInput(true);
            //OutputStream outputStream = httpURLConnection.getOutputStream();
            //BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            //String post_data = URLEncoder.encode("user_name""UTF-8")// HIER KUN JE VARIABELEN INGEVEN DIE WORDEN GEIMPLEMENTEERD IN HET PHP DOCUMENT
            //bufferedWriter.write();
            //bufferedWriter.flush();
            // bufferedWriter.close();
            //outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String result ="";
            String line="";
            while ((line=bufferedReader.readLine()) != null){
                result += line;
            }
            String[] separated = result.split(" ");
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("TijdsDuur");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}
}

还有我的MainActivity:

public class MainActivity extends AppCompatActivity {
TextView ViewBegin, ViewEind;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewBegin = (TextView) findViewById(R.id.txtViewBegin);
    ViewEind = (TextView) findViewById(R.id.txtViewEind);
}

public void onDraai(View view){
    String type = "Draai";

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type);

}
}

【问题讨论】:

    标签: php android mysql android-asynctask textview


    【解决方案1】:

    1.) 在您的BackgroundWorker 中为回调创建一个interface

    public interface DataCallBack{
         void onDataReceived(String s);
    }
    

    2.) 在 Activity 中实现该回调接口

    public class MainActivity extends AppCompatActivity implements BackgroundWorker.DataCallBack {
    

    3.) 任务完成后,通知您的活动并显示数据

    你的MainActivity 看起来像

    public class MainActivity extends AppCompatActivity implements BackgroundWorker.DataCallBack {
    TextView ViewBegin, ViewEind;
    
      // .....code
    
        @Override
        void onDataReceived(String s){
             if(s!=null){
                String[] separated = result.split(" ");
                ViewBegin.setText(separated[0]);  
                ViewEind.setText(separated[1]);  
             }    
        }
    
      // .....code
    
    }
    

    你的 BackgroundWorker 类看起来像

    public class BackgroundWorker extends AsyncTask<String,Void,String> {
        Context context;
        AlertDialog alertDialog;
        DataCallBack callback;
    
        BackgroundWorker(Context ctx){
            context = ctx;
    
            // initialize the callback reference
            callback = (DataCallBack) cxt;
        }
    
        public interface DataCallBack{
             void onDataReceived(String s);
        }
    
        // .....code
    
        @Override
        protected void onPostExecute(String result) {
            callback.onDataReceived(result);
            // ^^^ send data to Activity
    
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    
        // .....code
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多