2013-04-27 100 views
0

我不得不使用JSON输出,它基本上会显示随问题一起提问的问题数量。 以下JSON输出总结了什么,我想说:以编程方式通过循环添加textView或Expandable textView

{ "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来说很新,有人请帮忙。另一个问题是,如何访问textView上的不同功能,如android:backgroundandroid:layout_width等。 如果有另一种解决此问题的方法,我也非常开放。

P.S.我很抱歉,问题很长,但请帮忙。

回答

1

你想要一个ListViewGridView,并用你的CustomItem意见填充。你还需要一个CustomAdapter extends ArrayAdapter来填写你的物品清单。

CustomItem视图应该在XML中定义为视图。然后,在CustomAdapter中,您可以根据需要多次膨胀这些视图。 ListView只会自己请求一些视图和视图,只要它们具有适配器/ XML来备份它们,就可以与各种自定义数据一起使用。

考虑这种方法,给它一个镜头,回报。尽量避免像在Swing,AWT或QT中那样“以编程方式添加GUI” - 这是相当痛苦的。

我在我的答案中也有一些这种方法的例子。

+0

哇我没有想过这个......谢谢你的建议,我会很快回复,虽然使用这种方法,但不是现在,但很快就足够了。我现在还有其他工作要做......对此感到抱歉。 – 2013-04-27 16:05:43

+0

很酷。我明天可能也会在办公室;)很高兴帮助。 – Shark 2013-04-27 16:08:04

相关问题