1

我想在我的Fragment中添加一個新的TextView。這是我在的AsyncTask backgroundjob類代碼:設置新的TextView在AsyncTask中分段

protected void onPostExecute(String json) { 

    try { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray jsonArray = jsonObject.getJSONArray("server_response"); 
     JSONObject JO = jsonArray.getJSONObject(0); 
     String code = JO.getString("code"); 
     String message = JO.getString("message"); 

     if (code.equals("true")) { 
      /**********************************/ 

      LayoutInflater inflater = activity.getLayoutInflater(); 
      View mContainer = inflater.inflate(R.layout.fragment_home, null); 
      LinearLayout linearLayout = (LinearLayout)mContainer.findViewById(R.id.mylinearLayout); 

      TextView text = new TextView(linearLayout.getContext()); 
      text.setText("TEST"); 
      text.setTextSize(30); 

      Log.d("View", "Start"); 
      try { 
       linearLayout.addView(text); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

這是我的片段類:

public class Home extends Fragment { 

    public Home() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     TopicBackgroundTask backgroundTask = new TopicBackgroundTask(getActivity()); 
     backgroundTask.execute("yes"); 

     // Inflate the layout for this 

     return inflater.inflate(R.layout.fragment_home, container, false); 
    } 
} 

注意Home片段和TopicBackgroundTask是兩個不同的

這不適合我。誰能告訴我爲什麼?

回答

0

你並不需要在onPostExecute()

再次膨脹的佈局,您需要重新使用其在onCreateView()定佈局, 所以你必須要創建一個全局變量視圖, 設置在onCreateView()而不是直接返回它。

像 -
myView = inflater.inflate(R.layout.fragment_home, container, false);

onPostExecute()

LinearLayout linearLayout = (LinearLayout)mContainer.findViewById(R.id.mylinearLayout); 

    TextView text = new TextView(linearLayout.getContext()); 
       text.setText("TEST"); 
       text.setTextSize(30); 

       Log.d("View", "Start"); 
       try { 
        linearLayout.addView(text); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
+0

'Home'片段和'TopicBackgroundTask'是兩個不同的**類** +你不能從'onCreateView()'調用'View'我嘗試添加'getActivity()。setContentView(mContainer);'和它工作,但它顯示在片段之外 –

0

發現解決方案必須轉換的LayoutViewGroup

protected void onPostExecute(String json) { 

    try { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray jsonArray = jsonObject.getJSONArray("server_response"); 
     JSONObject JO = jsonArray.getJSONObject(0); 
     String code = JO.getString("code"); 
     String message = JO.getString("message"); 

     if (code.equals("true")) { 
      /**********************************/ 

      LayoutInflater inflater = activity.getLayoutInflater(); 
      View mContainer = inflater.inflate(R.layout.fragment_home, null); 
      // LinearLayout linearLayout = (LinearLayout)mContainer.findViewById(R.id.mylinearLayout); 

      TextView text = new TextView(linearLayout.getContext()); 
      text.setText("TEST"); 
      text.setTextSize(30); 

      Log.d("View", "Start"); 
      try { 
       ((ViewGroup)activity.findViewById(R.id.mylinearLayout)).addView(text); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      }