2014-08-28 49 views
2

以下this SO解決方案,我已經成功parsed JSON,但不知道如何同時顯示headings和TextViews points我如何顯示多個標題和點單活動

這是我JSON的樣子:

{ 
    "technology": [ 
     { 
      "title": "Android", 
      "description": [ 
       { 
        "heading": "Manufactures", 
        "points": [ 
         "Google", 
         "Samsung" 
        ] 
       }, 
       { 
        "heading": "Platforms", 
        "points": [ 
         "Kitkat", 
         "ICS" 
        ] 
       } 
      ] 
     } 
    ] 
} 

舉個例子,如果我這樣做tap on Android,然後我want to show是這樣的:

Manufactures 
[dot] Google 
[dot] Samsung 

Platforms 
[dot] Kitkat 
[dot] ICS 

對於reference看到這個圖片:

enter image description here

代替Why We Like It,我想顯示Manufactures並且代替Need to Know,我想顯示Platforms(以及它們各自的點),如圖所示。

gettingthis

Platforms 
[dot] ICS 

對於reference看到這個圖片:

enter image description here

那麼是什麼原因?以及我必須在我的代碼中進行更改?

DescriptionActivity.java:-

for(int s=0; s<arrayListDescription.size(); s++) 
    { 
     description = arrayListDescription.get(s); 
     strHeading = "&nbsp;" + description.getHeading(); 
     Log.d("heading::", strHeading); 
     arrayListPoints = description.getPoints();  

     for(int z=0; z<arrayListPoints.size(); z++) 
     { 
      String dot = getString(R.string.dot); 
      strPoints = ""; 
      strPoints += "&nbsp;&nbsp;" + dot + "&nbsp;" + arrayListPoints.get(z) + "\n"; 
      Log.d("points::", strPoints); 
     } 
     textViewPoints.setText(Html.fromHtml(strPoints)); 
    }  

    textViewHeading.setText(Html.fromHtml(strHeading)); 
    textViewHeading.setTextSize(20); 

} 

activity_description.xml: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textHeading" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    <TextView 
     android:id="@+id/textPoints" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

這就是我的Log說:

08-28 04:47:39.653: D/heading::(1902): Manufactures 
08-28 04:47:39.653: D/points::(1902): &emsp;&middot;Google 
08-28 04:47:39.671: D/points::(1902): &emsp;&middot;Samsung 
08-28 04:47:39.761: D/heading::(1902): Platforms 
08-28 04:47:39.761: D/points::(1902): &emsp;&middot;Kitkat 
08-28 04:47:39.761: D/points::(1902): &emsp;&middot;ICS 

通過using@ Dreagen的解決方案,gettingthis

Platforms 
null[dot]Google[dot]Samsung[dot]Kitkat[dot]ICS 

對於reference看到這個image

enter image description here

我用這個代碼:

for(int z=0; z<arrayListPoints.size(); z++) 
{ 
    String dot = getString(R.string.dot); 
    // strPoints += dot + arrayListPoints.get(z) + "\n"; 
    strPoints += "&emsp;&middot;" + arrayListPoints.get(z) + System.getProperty("line.separator"); 
    Log.d("points::", strPoints); 
} 

回答

1

活動

public class MainActivity extends ActionBarActivity { 

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

      ArrayList<Model> mydata = new ArrayList<Model>(); 
      // set 1 
      Model model = new Model(); 
      ArrayList<String> points = new ArrayList<String>(); 
      model.setHeading("Manufactures"); 
      points.add("Google"); 
      points.add("Samsung"); 
      model.setPoints(points); 
      mydata.add(model); 

      // set 2 

      model = new Model(); 
      points = new ArrayList<String>(); 
      model.setHeading("Platforms"); 
      points.add("Kitkat"); 
      points.add("ICS"); 
      model.setPoints(points); 
      mydata.add(model); 

      // String str = 
      // "<h4>An Unordered List:</h4><br/>&#8226; foo<br/>&#8226; bar<br/>&#8226; baz<br/>"; 

      TextView textview = (TextView) findViewById(R.id.myText); 
      String result = ""; 
      for (int i = 0; i < mydata.size(); i++) { 
       result += "<h4>" + mydata.get(i).getHeading() + "</h4>"; 
       ArrayList<String> appendPoints = new ArrayList<String>(mydata 
         .get(i).getPoints()); 
       for (int j = 0; j < appendPoints.size(); j++){ 
        result += "&#8226; " + appendPoints.get(j) + "<br/>";} 

      } 

      Spanned spannedResult = Html.fromHtml(result); 

      textview.setText(spannedResult); 

     } 

    } 

模型

public class Model { 

    private String heading; 
    private ArrayList<String> points; 

    public String getHeading() { 
     return heading; 
    } 

    public void setHeading(String heading) { 
     this.heading = heading; 
    } 

    public ArrayList<String> getPoints() { 
     return points; 
    } 

    public void setPoints(ArrayList<String> points) { 
     this.points = points; 
    } 

} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.nestedtext.MainActivity" > 

    <TextView 
     android:id="@+id/myText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

COMPLETE SOURCE CODE

+0

非常感謝你@DIVA – Mysterious 2014-08-28 12:17:43

+0

!!!!歡迎! – KOTIOS 2014-08-28 12:19:29

+0

嘿,我需要你的幫助,這很緊急!請儘快來@DIVA – Mysterious 2014-08-29 07:39:05

1

我想你問題是在這裏

for(int z=0; z<arrayListPoints.size(); z++) 
{ 
    strPoints = "&emsp;&middot;" + arrayListPoints.get(z) + "\n";   
    Log.d("points::", strPoints); 
} 

你只是覆蓋每次循環,這意味着在最後你只有最後一點在那裏strPoints。該日誌顯示兩者,因爲每次循環時都輸出到日誌。

將其更改爲:

for(int z=0; z<arrayListPoints.size(); z++) 
{ 
    strPoints += "&emsp;&middot;" + arrayListPoints.get(z) + System.getProperty("line.separator"); 
    Log.d("points::", strPoints); 
} 

注意+=,而不是僅僅=\n改變使用System.getProperty("line.separator"); 希望這有助於

+0

我刪除了我的答案。我完全誤解了這個問題。這個答案是正確的。 – schubakah 2014-08-28 09:28:26

+0

請檢查我的更新以上... – Mysterious 2014-08-28 09:41:56

+0

看起來你只是有一個問題,換行符。我會更新我的答案,以解決它的問題 – Dreagen 2014-08-28 09:45:39

0

除了Dreagen的答案,你應該更換新線符號到br標籤。 其他解決方案使用pre預格式化文本,但我不確定Android是否支持該標記。

+0

Android,不幸的是,它不支持'pre'標籤。來源:http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html – tolgap 2014-08-28 09:48:36

相關問題