在onPostExecute()中创建的Textview未在模拟器

时间:2016-12-21 13:58:52

标签: android android-layout

我在没有XML文件的情况下在android RadioGroups中创建动态EditTextMainActivity。我正在从数据库中获取数据,因为我使用AsyncTask类基于我在EditText方法中创建RadioGroupsonPostExecute的数据。要显示EditText我正在调用MainActivity ( lView = new LinearLayout(Main2Activity.this);)。但这些元素在模拟器中不可见..

提前致谢

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

LinearLayout lView未分配给R.layout.activity_main的视图,并且永远不会添加到根视图中。

您必须将其添加到R.layout.activity_main的根视图中。