2016-12-21 54 views
1

我正在創建動態RadioGroups,EditText在android MainActivity沒有XML文件。我正在從數據庫中獲取數據,因爲我使用的是AsyncTask類,這是基於我在創建那些數據時使用,RadioGroupsonPostExecute方法。事先顯示EditText我打電話MainActivity(lView = new LinearLayout(Main2Activity.this);)。但這些元件不可見模擬器..在onPostExecute()中創建Textview不顯示在模擬器中

由於

public class Main2Activity extends Activity { 


    private static final String Latest_Products = "Questions"; 
    JSONArray productsArray = null; 

    StringBuilder result; 

    public static final int CONNECTION_TIMEOUT = 10000; 
    public static final int READ_TIMEOUT = 15000; 

    bean b = null; 

    LinearLayout lView = null; 
    private List<EditText> le = null; 
    HashMap<String, bean> hm = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new Asyncchk().execute(); 
    } 


    private class Asyncchk extends AsyncTask<String, String, StringBuilder> { 
     ProgressDialog pdLoading = new ProgressDialog(Main2Activity.this); 
     HttpURLConnection conn; 
     URL url = null; 

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

      //this method will be running on UI thread 
      pdLoading.setMessage("\tLoading..."); 
      pdLoading.setCancelable(false); 
      pdLoading.show(); 

     } 

     @Override 
     protected StringBuilder doInBackground(String... param) { 

      try { 

       // Enter URL address where your php file resides 
       url = new URL("http://192.168.1.33/app/alldata.php"); 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(getApplicationContext(), "URL Exception", Toast.LENGTH_LONG).show(); 

       e.printStackTrace(); 

      } 
      try { 
       // Setup HttpURLConnection class to send and receive data from php and mysql 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setReadTimeout(READ_TIMEOUT); 
       conn.setConnectTimeout(CONNECTION_TIMEOUT); 
       conn.setRequestMethod("POST"); 

       // setDoInput and setDoOutput method depict handling of both send and receive 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 



       // Append parameters to URL 
       Uri.Builder builder = new Uri.Builder() 
         .appendQueryParameter("user_id", "user_id") 
         .appendQueryParameter("dpt_id", "dptid"); 
       String query = builder.build().getEncodedQuery(); 

       // Open connection for sending data 
       OutputStream os = conn.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
       writer.write(query); 
       writer.flush(); 
       writer.close(); 
       os.close(); 
       conn.connect(); 

      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       Log.e("error","error"); 
       e1.printStackTrace(); 


      } 

      try { 

       int response_code = conn.getResponseCode(); 

       // Check if successful connection made 
       if (response_code == HttpURLConnection.HTTP_OK) { 

        // Read data sent from server 
        InputStream input = conn.getInputStream(); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 

        result = new StringBuilder(); 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
        String next1; 
        while ((next1 = bufferedReader.readLine()) != null) { 

         result.append(next1 + "\n"); 

        } 


       } 

      } catch (IOException e) { 

       e.printStackTrace(); 


      } finally { 
       conn.disconnect(); 
      } 

      return result; 

     } 

     @Override 
     protected void onPostExecute(StringBuilder s) { 
      super.onPostExecute(s); 


      try{ 
       JSONArray login; 
       JSONObject obj=new JSONObject(s.toString()); 

       if(s.toString().contains("Result")) { 
        login = obj.getJSONArray("Result"); 

        hm = new HashMap<String, bean>(); 
        le = new ArrayList<EditText>(); 

        lView = new LinearLayout(Main2Activity.this); 
        for(int i=0;i<login.length();i++) 
        { 

         JSONObject c = login.getJSONObject(i); 

         Log.e("length",c.toString()); 

         productsArray = c.getJSONArray(Latest_Products); 

         for (int j = 0; j < productsArray.length(); j++) { 

          JSONObject cc = productsArray.getJSONObject(j); 

          b = new bean(); 

          if(cc.getString("q_type").equalsIgnoreCase("1")){ 

           b.setQno(i); 
           b.setQtype(1); 
           b.setQues(cc.getString("question")); 
           TextView t1 = new TextView(Main2Activity.this); 
           t1.setText(cc.getString("question")); 

           EditText e1 = new EditText(Main2Activity.this); 
           e1.setWidth(150); 

           lView.addView(t1); 
           lView.addView(e1); 

           lView.setOrientation(LinearLayout.VERTICAL); 

           le.add(e1); 

           hm.put("1", b); 

          } 
         } 

        } 

       } 

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

      } 

      pdLoading.dismiss(); 
     } 
    } 
} 

回答

0

的LinearLayout LVIEW沒有分配給所述R.layout.activity_main和它的圖永遠不會添加到根視圖。

您必須將其添加到R.layout.activity_main的根視圖。

+0

我需要添加哪裏? –

+0

如果你想使lView LinearLayout的根視圖添加'setContentView(lView);'在'onPostExecute'結束時 – Hamilton

+0

#哈密爾頓謝謝你哥們:)它爲我工作 –

相關問題