0

我必须使用一个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();
         }

textView在哪里question_answer_view

并且为此的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:backgroundorandroid:layout_width等​​。如果有另一种方法来解决这个问题,我也对此非常开放。

PS我很抱歉,这个问题很长,但请帮忙。

4

1 回答 1

1

您想要 aListView或 a GridView,并用您的CustomItem视图填充它。您还需CustomAdapter extends ArrayAdapter要用您的物品填写上述清单。

CustomItem视图应该在 XML 中定义为视图。然后,CustomAdapter您可以根据需要多次扩充这些视图。ListView 只会请求视图和视图本身的数量,只要它们有适配器/XML 来支持它们,它就可以与各种自定义数据一起工作。

考虑这种方法,试一试,然后报告。请尽量避免像在 Swing、AWT 或 QT 中那样“以编程方式添加 GUI”——这相当痛苦。

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

于 2013-04-27T16:00:34.327 回答