Android 通过循环以编程方式添加textView或可扩展的textView
我必须使用一个Android 通过循环以编程方式添加textView或可扩展的textView,android,json,loops,textview,Android,Json,Loops,Textview,我必须使用一个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", "categor
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();
}
因此,我现在要做的是为每个问答线程安排一个单独的文本视图,因为问答线程对于应用程序的每个用户都是唯一的,所以在xml文件中设置它没有多大意义。最终,我想我会希望以编程方式生成textView,但我不知道自己是android新手,请有人帮忙。另一个问题是如何通过这种方式访问textView上的不同功能,如android:background
或android:layout\u width
等。
如果有其他方法解决这个问题,我也非常愿意接受
另外,我很抱歉,问题很长,但请帮帮忙。您需要一个
列表视图
或网格视图
,并用自定义项
视图填充它。您还需要一个CustomAdapter extends ArrayAdapter
来用您的项目填充上述列表
CustomItem
视图应在XML中定义为视图。然后,在CustomAdapter
中,根据需要多次膨胀这些视图。ListView将只请求足够数量的视图和视图本身,以使其能够与各种定制数据一起开箱即用,只要它们有适配器/XML来备份它们
考虑一下这种方法,试一试,然后报告。尽量避免像在Swing、AWT或QT中那样“以编程方式添加GUI”——这相当痛苦
我的答案中也有一些这种方法的例子。哇,我没想到。。。谢谢你的建议,我会很快回复,虽然使用这种方法,不是现在,而是足够快。实际上我现在还有其他工作要做。。。对不起,很酷。我明天可能也在办公室;)很乐意帮忙。
<?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>