2017-06-19 76 views
0

我在Android中使用Json 1.9.2庫來解析網站中的數據,然後將結果數據設置爲Android中的TextView。但我無法在TextView上設置獲取的數據。來自Json查詢的數據未在Android Text View中填充

分享相關的代碼。我正在使用片段。

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    tv1 = (EditText) getActivity().findViewById(R.id.trending_textView); 

    new Title().execute(); 


} 

private class Title extends AsyncTask<Void, Void, Void> { 
    String title; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     mProgressDialog = new ProgressDialog(getActivity()); 
     mProgressDialog.setTitle("Fetching the latest trends"); 
     mProgressDialog.setMessage("#Trends"); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.show(); 
    } 

    @Override 
    public Void doInBackground(Void... params) { 
     try { 

      doc = Jsoup.connect(url).get(); 

      Elements links = doc.getElementsByTag("a"); 

      for (Element link : links) { 
       //String linkHref = link.attr("href"); 
       linkText = link.text(); 

       //System.out.println("#"+linkText); 
      } 

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

    @Override 
    public void onPostExecute(Void result) { 

     tv1.setText(linkText.toString()); 
     mProgressDialog.dismiss(); 

    } 
} 

任何幫助將不勝感激。

+0

是否'mProgressDialog.dismiss()'工作? – Zakir

+0

'linkText'在'doInBackground'中打印什麼? – Zakir

+0

我刪除循環後問題得到解決 – CodePlorer

回答

0

您必須通過返回doInbackground()方法 中的結果並將結果作爲參數接收onPostExecute()中的結果,然後從那裏填充視圖,因爲onPostExecute()運行時將任務結果發佈到UI線程中UI線程 更多信息: -

https://developer.android.com/reference/android/os/AsyncTask.html

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    tv1 = (EditText) getActivity().findViewById(R.id.trending_textView); 

    new Title().execute(); 


} 

private class Title extends AsyncTask<Void, Void, Elements> { 
    String title; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     mProgressDialog = new ProgressDialog(getActivity()); 
     mProgressDialog.setTitle("Fetching the latest trends"); 
     mProgressDialog.setMessage("#Trends"); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.show(); 
    } 

    @Override 
    public Elements doInBackground(Void... params) { 
     Elements links ; 
     try { 

      doc = Jsoup.connect(url).get(); 

      links = doc.getElementsByTag("a"); 

      } catch (Exeption e) { 

       return null ; 
     } 


     return links; 
    } 

    @Override 
    public void onPostExecute(Elements ...links) { 

      if(links.get(0) == null) return ; 
      for (Element link : links.get(0)) { 
      //String linkHref = link.attr("href"); 
      linkText = link.text(); 
      // u might add scrolling behavior 
      tv.append(linktext + "\n"); 
      //System.out.println("#"+linkText); 
     } 


    } 
}