Saturday, April 27, 2013

[android help] Adding a textView or an Expandable textView programmatically through looping


I had to work with a JSON output that basically would show me the number of questions asked along with the reply that I am getting. The following JSON output sums up what I am trying to say:



{ "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"
}


So what I had to do was show this in a very presentable way, and what I did was used HTML type formatting to display the result, where I presented the question in a bold typeface, and then I presented the answer in a italic typeface. The code is as follows:



try{

jArray = jObj.getJSONArray("data");
for(int i = 0; i {
jObj2 = jArray.getJSONObject(i);
if(result_JSON.equalsIgnoreCase(""))
result_JSON = ""+jObj2.getString("question")+"
";
else
result_JSON = result_JSON+""+jObj2.getString("question")+"
";
if(jObj2.getString("answer").equalsIgnoreCase("null"))
result_JSON = result_JSON+"We will get back to you soon
";
else
result_JSON = result_JSON+""+jObj2.getString("answer")+"
";
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();
}


where question_answer_view is a textView.


and the xml file for this is:




android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#eeeeee" >


android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical">

android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" >

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"/>




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"/>








So what I would like to do now is to arrange for each question and answer thread, a separate textView, and because the question and answer thread is unique to each user of the application, setting it up in the xml file would not make much sense. Ultimately I thought I would want to generate the textView programmatically, but I have no idea being very new to android, someone please help. And the other question is how do I access different features on textView this way like the android:background or android:layout_width etc. If there is another approach to this problem, I am very open to that too.


P.S. I do apologize, the question is very long, but please do help.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...