2017-02-10 21 views
1

我對android非常陌生,不能從屏幕上的數據庫獲取結果到TextView。目前我的應用程序在點擊一個按鈕後返回一條消息。我在屏幕txtViewBegintxtViewEind上有兩個TextView元素。從mySql(通過php)我得到一個像這樣的字符串22-02-2014 24-05-2017,我把它分成Strings (.split(" "))的數組。如何將BackgroundWorker的結果設置爲TextView

我可以使用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); 

} 
} 

回答

2

1)在你的BackgroundWorker

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

2)實施創造回調的interface回調界面在你的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 

} 
相關問題