2013-06-03 58 views
0

我一直在搞這個代碼大約一天,我似乎無法弄清楚發生了什麼問題。我試圖收到一些JSON,然後從響應中動態創建Views。我會發布代碼,然後解釋問題。無法從JSON創建視圖

的AsyncTask

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> 

logcat的

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()方法時,它會有一個值。現在它顯然沒有。

儘管我認爲我知道這個問題,但我找不出原因。任何幫助或洞察力,表示讚賞。

編輯,以顯示更改

回答

2

「爲什麼總是編輯文本?」 - 原因是你的比較:

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

在java中的字符串比較與.equals()完成的,所以你想是這樣的:

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

「爲什麼不把它添加他們嗎?」 - 原因可能是你的佈局。 ScrollView s是垂直的。孩子實際上是一個水平的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」。不知道你怎麼可以添加任何兄弟姐妹,這將是可見的。所以你要麼希望自己的滾動型是一個Horizo​​ntalScrollView或者你希望你的LL實際上是垂直...

編輯:

東西其他錯誤,我注意到,您只使用一個「結果」變量:

MyResult theResult = null; 

你保持覆蓋每次都發現了新的元素。您需要保留一個List或一組結果。 onPostExecute()只會針對整個異步操作運行一次。

+0

仍然無法正常工作。編輯我的帖子以顯示更改。你對字符串比較絕對正確。我不知道爲什麼我試圖比較它。我也將我的genA_layout更改爲垂直。 –

+0

MyResult是一個類,我相信你知道這一點。我使用它,所以我可以保持變量的範圍,並在'postExecute()'中執行它們。我認爲你每一次被覆蓋的事實都是正確的。但是,如果這是真的,它不會創建至少一個'View'?你會推薦我如何去實例化'ArrayList'? –

+0

好吧我取得了一些進展,我現在知道MyResult類永遠不會被任何值填充。我會發布另一個問題,因爲這個問題已經超出了原始問題的範圍 –