2016-12-21 5 views
1

RadioGroupsEditTextMainActivityのXMLファイルを動的に作成していました。私はAsyncTaskクラスを使って、私が作成したデータに基づいて、EditTextRadioGroupsonPostExecuteの方法で使用しているというデータをデータベースから取得しています。事前に私はこれらの要素.but MainActivity(lView = new LinearLayout(Main2Activity.this);)はエミュレータでは表示されません呼んでいたEditText ..エミュレータに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

#ハミルトンありがとうございました:)私のために働いています –

関連する問題