我必须使用JSON
输出,它基本上会向我显示所提问题的数量以及我得到的回复。
以下JSON
输出总结了我想说的内容:
{ "data" : [ { "answer" : "You have better opportunities if you go abroad. If you are interested in IT, then go for it. You will do better in IT.",
"category_id" : "1",
"category_name" : "Education",
"created_on" : "25 Apr, 2013 11:45",
"is_answered" : true,
"modified_on" : "26 Apr, 2013 07:20",
"parent_id" : "0",
"question" : "I am thinking of continuing my further studies. Is it a good for me to apply aborad or study here?",
"question_id" : "2",
"user_id" : "17"
},
{ "answer" : "According to your chart, you are facing the malefic affect of the planet Saturn. You can reduce this malefic affect by offering water to Peepal tree on Saturdays. ",
"category_id" : "3",
"category_name" : "Health",
"created_on" : "25 Apr, 2013 20:21",
"is_answered" : true,
"modified_on" : "26 Apr, 2013 11:49",
"parent_id" : "0",
"question" : "I am having a trouble in my business. What should i do?",
"question_id" : "3",
"user_id" : "17"
},
{ "answer" : "Your question has been posted to blamethestars.com. We wil get back to you soon.",
"category_id" : "2",
"category_name" : "Career",
"created_on" : "26 Apr, 2013 11:21",
"is_answered" : false,
"modified_on" : "",
"parent_id" : "0",
"question" : "what is the best field of work for me?",
"question_id" : "4",
"user_id" : "17"
}
],
"message" : null,
"status" : "success"
}
所以我必须做的是以一种非常明显的方式展示这一点,我所做的是使用HTML类型格式来显示结果,我用粗体字体显示问题,并且然后我用斜体字体提出答案。代码如下:
try{
jArray = jObj.getJSONArray("data");
for(int i = 0; i<jArray.length();i++)
{
jObj2 = jArray.getJSONObject(i);
if(result_JSON.equalsIgnoreCase(""))
result_JSON = "<b><font color =\"#6C2C6B\">"+jObj2.getString("question")+"</font></b><br/>";
else
result_JSON = result_JSON+"<b><font color =\"#6C2C6B\">"+jObj2.getString("question")+"</font></b><br/>";
if(jObj2.getString("answer").equalsIgnoreCase("null"))
result_JSON = result_JSON+"<i>We will get back to you soon</i><br/>";
else
result_JSON = result_JSON+"<i>"+jObj2.getString("answer")+"</i><br/>";
Log.i("QUESTION", result_JSON);
}
question_answer_view.setText(Html.fromHtml(result_JSON));
if(question_answer_view.getText().toString().length()<1)
{
question_answer_view.setText("NO Q & A TO DISPLAY");
Toast.makeText(getApplicationContext(), "No questions asked till now.", Toast.LENGTH_SHORT).show();
}
其中question_answer_view
是textView。
并且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"
android:background="#eeeeee" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Question and Answers"
android:textSize="20sp"
android:textAllCaps="true"
android:textColor="#6C2C6B"
android:gravity="center_horizontal"/>
</LinearLayout>
<TextView
android:id="@+id/question_answer_stack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:background="#ffffff"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
所以我现在要做的是安排每个问题和答案线程,一个单独的textView,并且因为问题和答案线程对于应用程序的每个用户都是唯一的,所以在xml文件中设置它不会很有意义。最终我以为我想以编程方式生成textView,但我不知道是非常新的android,有人请帮忙。另一个问题是我如何以android:background
或android:layout_width
等方式访问textView上的不同功能。
如果还有另一种解决这个问题的方法,我也对此持开放态度。
P.S。我道歉,问题很长,但请帮忙。
答案 0 :(得分:1)
您需要ListView
或GridView
,并使用您的CustomItem
观点进行填充。您还需要CustomAdapter extends ArrayAdapter
来填充上述列表中的商品。
CustomItem
视图应该在XML中定义为视图。然后,在CustomAdapter
中,您可以根据需要多次展开这些观看次数。 ListView只会请求多个视图和视图本身,只要它们具有适配器/ XML来备份它们,它就可以与各种自定义数据一起使用。
考虑这种方法,试一试,报告回来。尽量避免像在Swing,AWT或QT中那样“以编程方式添加GUI” - 这很痛苦。
我的答案中也有一些这种方法的例子。