0

我已经把这段代码弄乱了大约一天,我似乎无法弄清楚出了什么问题。我正在尝试接收一些JSON,然后Views从响应中动态创建。我将发布代码,然后解释问题。

异步任务

private static final String TAG_SUCCESS = "success";
private static final String TAG_QUESTIONS = "questions";
private static final String TAG_NAME = "display_name";
private static final String TAG_FIELD = "field_type";
private static final String TAG_VALUE = "option_value"; 

private String r = "Radio";

class LoadAllQuestions extends AsyncTask<String, String, MyResult> {

    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();
    JSONArray questions = null;
    MyResult theResult = null;

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading questions. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected MyResult doInBackground(String... args) {

        // getting JSON string from URL
        companyName = cn.getText().toString();
        projectName = pn.getText().toString();
        String componentName = (String) ab.getSelectedTab().getText();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("company", companyName));
        nameValuePairs.add(new BasicNameValuePair("project", projectName));
        nameValuePairs.add(new BasicNameValuePair("component",
                componentName));

        JSONObject json = jParser.makeHttpRequest(url, "POST",
                nameValuePairs);

        // Check your log cat for JSON response
        Log.d("All Questions: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                Log.v("RESPONSE", "Success!");
                // products found: getting Array of Questions
                questions = json.getJSONArray(TAG_QUESTIONS);

                // looping through All Questions
                for (int i = 0; i < questions.length(); i++) {

                    JSONObject c = questions.getJSONObject(i);

                    // Storing each JSON item in variable
                    String name = c.getString(TAG_NAME);
                    String field = c.getString(TAG_FIELD);
                    String value = c.getString(TAG_VALUE);

                    if (r.equals(field)) {
                        Log.v("RESPONSE", "oh look a radio button");
                        theResult = new MyResult();
                        theResult.setType(1);
                        theResult.setName(name);
                        theResult.setField(field);
                        theResult.setValue(value);

                    } else if (et.equals(field)){
                        Log.v("RESPONSE", "just another EditText");
                        theResult = new MyResult();
                        theResult.setType(2);
                        theResult.setName(name);
                        theResult.setField(field);
                        theResult.setValue(value);
                    } else {

                    }

                }
            } else {
                // no products found
                Log.v("ERROR", "No JSON for you!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return theResult;
    }

    protected void onPostExecute(MyResult theResult) {
        // dismiss the dialog
        pDialog.dismiss();
        // if the answer should be a radio button, inflate it
        if (theResult.getType() == 1) {
            Log.v("RESPONSE", "About to create a radio button");
            // find
            LinearLayout content = (LinearLayout) view
                    .findViewById(R.id.genA_layout);
            // create
            TextView tv = new TextView(getActivity());
            RadioGroup rg = new RadioGroup(getActivity());
            rg.setOrientation(RadioGroup.HORIZONTAL);
            RadioButton rb = new RadioButton(getActivity());
            RadioButton rb2 = new RadioButton(getActivity());
            LinearLayout ll = new LinearLayout(getActivity());

            // set
            rb.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            rb2.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            ll.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            rb.setText(theResult.getValue());
            Log.v("RESPONSE", theResult.getValue());
            rb2.setText(theResult.getValue());              
            tv.setText(theResult.getName());
            Log.v("RESPONSE", theResult.getName());
            ll.setOrientation(LinearLayout.HORIZONTAL);
            // add
            rg.addView(rb);
            rg.addView(rb2);
            ll.addView(tv);
            ll.addView(rg);
            content.addView(ll);
        }
        // else inflate the view as an EditText field
        else if (theResult.getType() == 2) {
            Log.v("RESPONSE", "About to create an EditText");
            // find
            LinearLayout content = (LinearLayout) view
                    .findViewById(R.id.genA_layout);
            // create
            TextView tv = new TextView(getActivity());
            EditText et = new EditText(getActivity());
            LinearLayout ll1 = new LinearLayout(getActivity());
            // set
            tv.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            et.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            ll1.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            tv.setText(theResult.getName());
            Log.v("RESPONSE", theResult.getName());
            ll1.setOrientation(LinearLayout.HORIZONTAL);
            // add
            ll1.addView(tv);
            ll1.addView(et);
            content.addView(ll1);
        }

        // find
        LinearLayout loader = (LinearLayout) view
                .findViewById(R.id.loader_layout);
        Button save = (Button) view
                .findViewById(R.id.generalAssets_save_button_ID);
        // set
        loader.setVisibility(View.GONE);
        save.setVisibility(View.VISIBLE);

    };
}

XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gen_assets"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/twoglobe_line"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/genA_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/loader_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:gravity="center"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/info_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/company_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/company_name" />

                <EditText
                    android:id="@+id/company_input"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/info_layout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/project_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/project_name" />

                <EditText
                    android:id="@+id/project_input"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10" />
            </LinearLayout>

            <Button
                android:id="@+id/generalAssets_load_button_ID"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/load" />

            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" >
            </ListView>
        </LinearLayout>

        <Button
            android:id="@+id/generalAssets_save_button_ID"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/save"
            android:visibility="gone" />
    </LinearLayout>

</ScrollView>

日志猫

    06-03 16:04:51.948: E/json data(4103): json result {"questions":[{"display_name":"Store #","field_type":"Text Field","option_value":""},{"display_name":"Address","field_type":"Text Field","option_value":""},{"display_name":"Type of Business","field_type":"Drop Down Menu","option_value":"Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther"},{"display_name":"Is this business good?","field_type":"Radio","option_value":"Yes\r\nNo"},{"display_name":"Are they nice people?","field_type":"Check Box","option_value":"Yes\r\nNo"}],"success":1}
06-03 16:04:51.958: D/All Questions:(4103): {"success":1,"questions":[{"option_value":"","field_type":"Text Field","display_name":"Store #"},{"option_value":"","field_type":"Text Field","display_name":"Address"},{"option_value":"Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther","field_type":"Drop Down Menu","display_name":"Type of Business"},{"option_value":"Yes\r\nNo","field_type":"Radio","display_name":"Is this business good?"},{"option_value":"Yes\r\nNo","field_type":"Check Box","display_name":"Are they nice people?"}]}
06-03 16:04:51.958: V/RESPONSE(4103): Success!
06-03 16:04:51.958: V/RESPONSE(4103): just another EditText
06-03 16:04:51.958: V/RESPONSE(4103): just another EditText
06-03 16:04:51.958: V/RESPONSE(4103): oh look a radio button

现在的问题是没有一个Views正在创建。正如您从logcat帖子中看到的那样,应用程序会提取JSON响应,然后识别它们。但是,它永远不会达到onPostExecute()创建Views. 在进行更改后@dmon 建议我必须认为我存储的结果超出了范围。否则,当我调用我的theResult.getType()方法时,它会有一个值。现在显然没有。

尽管我认为我知道问题所在,但我不知道为什么。任何帮助或见解表示赞赏。

编辑以显示更改

4

1 回答 1

2

“为什么总是编辑文本?” - 原因是你的比较:

 if (field == r)  //where r is "Radio"

java中的字符串比较是用 完成的.equals(),所以你想要这样的东西:

 if ("Radio".equals(field))  { ... }

“为什么不添加它们?” - 原因可能是你的布局。ScrollViews 是垂直的。child实际上是一个水平的LL:

  <LinearLayout
    android:id="@+id/genA_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal" >

然后它的第一个孩子的宽度为“match_parent”。不确定如何将任何兄弟姐妹添加到可见的...所以您要么希望 ScrollView 成为 Horizo​​ntalScrollView 要么希望您的 LL 实际上是垂直的...

编辑:

我注意到还有其他错误,您只使用了一个“结果”变量:

 MyResult theResult = null;

每次找到新元素时都会覆盖它。您需要保留一个列表或结果数组。onPostExecute()整个异步操作只会运行一次。

于 2013-06-03T20:01:38.970 回答