我正在关注this SO解决方案,并且我已成功parsed JSON
,但我不知道如何在TextViews中显示headings
和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
,请参阅此图片:
代替Why We Like It
,我想显示Manufactures
并代替Need to Know
,我想显示Platforms
(及其各自的点数),如图所示
但getting
类似于this
:
Platforms
[dot] ICS
对于reference
,请参阅此图片:
那是什么原因?我必须在我的代码中进行更改?
DescriptionActivity.java: -
for(int s=0; s<arrayListDescription.size(); s++)
{
description = arrayListDescription.get(s);
strHeading = " " + 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 += " " + dot + " " + 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):  ·Google
08-28 04:47:39.671: D/points::(1902):  ·Samsung
08-28 04:47:39.761: D/heading::(1902): Platforms
08-28 04:47:39.761: D/points::(1902):  ·Kitkat
08-28 04:47:39.761: D/points::(1902):  ·ICS
通过using
@ Dreagen 解决方案,getting
类似this
:
Platforms
null[dot]Google[dot]Samsung[dot]Kitkat[dot]ICS
对于reference
,请参阅此image
:
我使用了这段代码:
for(int z=0; z<arrayListPoints.size(); z++)
{
String dot = getString(R.string.dot);
// strPoints += dot + arrayListPoints.get(z) + "\n";
strPoints += " ·" + arrayListPoints.get(z) + System.getProperty("line.separator");
Log.d("points::", strPoints);
}
答案 0 :(得分:1)
我认为你的问题在这里
for(int z=0; z<arrayListPoints.size(); z++)
{
strPoints = " ·" + arrayListPoints.get(z) + "\n";
Log.d("points::", strPoints);
}
每次循环时,你只是覆盖strPoints
,这意味着最后你只有最后一点。日志显示两者,因为每次循环时都会输出到日志。
将其更改为:
for(int z=0; z<arrayListPoints.size(); z++)
{
strPoints += " ·" + arrayListPoints.get(z) + System.getProperty("line.separator");
Log.d("points::", strPoints);
}
请注意+=
而不只是=
以及使用\n
System.getProperty("line.separator");
的更改
希望这有帮助
答案 1 :(得分: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/>• foo<br/>• bar<br/>• 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 += "• " + 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>
答案 2 :(得分:0)
除了Dreagen的回答,您还应该将新的线路符号更改为br
标签。 其他解决方案是使用 pre
预格式文本,但我不确定Android是否支持该标记。