嗨我需要获得动态字符串并需要存储在textview中(id是在xml中的trainer_title_text)。这该怎么做? json的响应输出是: 1.私人培训师2.政府培训师。如何在不改变xml对齐的情况下在textview中指定字符串列表?
这是代码:
public class DynamicTextView extends Fragment {
private TextView TitleTypes;
private String StrDescription;
private RelativeLayout relative;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.trainers_certification,
container, false);
JSONArray jsonTypePrice=jsonData.getJSONArray("trainer_type_items");
for (int i = 0; i < jsonTypePrice.length(); i++) {
JSONObject object = jsonTypePrice.getJSONObject(i);
strType=object.getString("type");
//here i am getting type as 1.private Test 2.public Test
strCost=object.getString("cost");
Log.e("service call types", strType);
}
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(lparams);
relative.addView(textView);
textView.setText(strType);
I used like this but it's always setting only one data.
Output is:
private Test
Here is xml: where id is trainer_title_text i need to assign list of strings in vertical.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/product_bg" >
<RelativeLayout
android:id="@+id/relate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >
<RelativeLayout
android:id="@+id/product_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" >
<TextView
android:id="@+id/trainer_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Title:Chairperson,Department of Physical Education" />
<RelativeLayout
android:id="@+id/product_image"
android:layout_width="match_parent"
android:layout_height="@dimen/product_main_image_height"
android:layout_below="@+id/trainer_description"
android:background="@drawable/custom_border"
android:gravity="center"
android:visibility="gone" >
<ImageView
android:id="@+id/product_main_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="@android:color/transparent"
android:onClick="onclick"
android:padding="10dp"
android:src="@drawable/default_thumb" />
</RelativeLayout>
<com.contus.sportscorner.views.HorizontalListView
android:id="@+id/product_images"
android:layout_width="match_parent"
android:layout_height="@dimen/product_list_image_size"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/product_image"
android:layout_marginTop="10dp"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/desc_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/product_images"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:padding="5dp" >
<TextView
android:id="@+id/trainer_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"
android:padding="10dp"
android:text="Types of Trainer"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="visible" />
<View
android:id="@+id/view_divider_desc"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/trainer_title"
android:layout_marginTop="1dp"
android:background="@color/blue" />
<TextView
android:id="@+id/trainer_title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view_divider_desc"
android:layout_marginTop="10dp"
android:text="Private"
android:textColor="@color/sub_textcolor" />
<RelativeLayout
android:id="@+id/event_info_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/trainer_title_text"
android:layout_marginTop="10dp"
android:background="@android:color/white" >
<TextView
android:id="@+id/event_info_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="Cost for his Services"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="visible" />
<View
android:id="@+id/event_info_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/event_info_title"
android:layout_marginTop="1dp"
android:background="@color/blue" />
<View
android:id="@+id/event_calendar_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/event_info_divider"
android:background="#FFFFFF" />
<RelativeLayout
android:id="@+id/event_time_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/event_calendar_divider"
android:background="#E9FDE8"
android:padding="5dp" >
<TextView
android:id="@+id/event_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingLeft="5dp"
android:text="Private"
android:textColor="@color/sub_textcolor" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="QAR650.00" />
</RelativeLayout>
<View
android:id="@+id/event_street_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/event_time_holder"
android:background="#FFFFFF" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/event_info_holders"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/event_info_holder"
android:layout_marginTop="10dp"
android:background="@android:color/white" >
<TextView
android:id="@+id/event_info_titles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="Discounts"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="visible" />
<View
android:id="@+id/event_info_dividers"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/event_info_titles"
android:layout_marginTop="1dp"
android:background="@color/blue" />
<View
android:id="@+id/event_calendar_dividers"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/event_info_dividers"
android:background="#FFFFFF" />
<RelativeLayout
android:id="@+id/event_time_holders"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/event_calendar_dividers"
android:background="#E9FDE8"
android:padding="5dp" >
<TextView
android:id="@+id/event_times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingLeft="5dp"
android:text="Students"
android:textColor="@color/sub_textcolor" />
<TextView
android:id="@+id/prices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="20%" />
</RelativeLayout>
<View
android:id="@+id/event_street_dividers"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/event_time_holders"
android:background="#FFFFFF" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<View
android:id="@+id/event_contact_dividers"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/product_main"
android:layout_marginTop="1dp"
android:background="@drawable/title_bg" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/event_contact_dividers"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<Button
android:id="@+id/signInButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="@drawable/button_radius"
android:text="Book an Appointment"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<Button
android:id="@+id/food_and_health"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="5dp"
android:background="@drawable/button_radius"
android:text="Food and health tips and tricks"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:0)
使用StringBuilder收集以这种方式显示所需的所有信息:
JSONArray jsonTypePrice=jsonData.getJSONArray("trainer_type_items");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < jsonTypePrice.length(); i++) {
JSONObject object = jsonTypePrice.getJSONObject(i);
strType=object.getString("type");
// Note the new-line we're adding at the end
builder.append(strType).append("\n");
strCost=object.getString("cost");
Log.e("service call types", strType);
}
然后只需将我们构建的多行文本添加到textView。
textView.setText(builder.toString());