2014-08-28 44 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

相关问题