所以我的应用程序有点问题。我将相同的布局11次添加到我的主布局,在这里我向他们添加文字。问题是,没有文字显示。对不起我不完美的英语。 :d
JSONParser parser = new JSONParser();
HashMap<String, String> params = new HashMap<>();
params.put("username", username);
params.put("pass", password);
JSONObject json = parser.makeHttpRequest(link to php file, params);
ArrayList<String> stundenList = new ArrayList<>();
int success = json.getInt("success");
if (success == 1) {
JSONArray stunden = json.getJSONArray("stundenplan");
for (int i = 0; i < stunden.length(); i++) {
JSONObject c = stunden.getJSONObject(i);
String montag = c.getString("montag");
String dienstag = c.getString("dienstag");
String mittwoch = c.getString("mittwoch");
String donnerstag = c.getString("donnerstag");
String freitag = c.getString("freitag");
stundenList.add(montag);
stundenList.add(dienstag);
stundenList.add(mittwoch);
stundenList.add(donnerstag);
stundenList.add(freitag);
}
for(int i = 0; i < getRows().size(); i++) {
LinearLayout ll = (LinearLayout) Karo.main.findViewById(getRows().get(i));
TextView column1 = (TextView) ll.findViewById(R.id.column1);
TextView column2 = (TextView) ll.findViewById(R.id.column1);
TextView column3 = (TextView) ll.findViewById(R.id.column1);
TextView column4 = (TextView) ll.findViewById(R.id.column1);
TextView column5 = (TextView) ll.findViewById(R.id.column1);
TextView column6 = (TextView) ll.findViewById(R.id.column1);
TextView column7 = (TextView) ll.findViewById(R.id.column1);
TextView column8 = (TextView) ll.findViewById(R.id.column1);
TextView column9 = (TextView) ll.findViewById(R.id.column1);
TextView column10 = (TextView) ll.findViewById(R.id.column1);
TextView column11 = (TextView) ll.findViewById(R.id.column1);
column1.setText(stundenList.get(i).split(";")[0]);
column2.setText(stundenList.get(i).split(";")[1]);
column3.setText(stundenList.get(i).split(";")[2]);
column4.setText(stundenList.get(i).split(";")[3]);
column5.setText(stundenList.get(i).split(";")[4]);
column6.setText(stundenList.get(i).split(";")[5]);
column7.setText(stundenList.get(i).split(";")[6]);
column8.setText(stundenList.get(i).split(";")[7]);
column9.setText(stundenList.get(i).split(";")[8]);
column10.setText(stundenList.get(i).split(";")[9]);
column11.setText(stundenList.get(i).split(";")[10]);
}
}
getRows函数:
private static List<Integer> getRows() {
List<Integer> rows = new ArrayList<>();
rows.add(R.id.stundenplan_montag);
rows.add(R.id.stundenplan_dienstag);
rows.add(R.id.stundenplan_mittwoch);
rows.add(R.id.stundenplan_donnerstag);
rows.add(R.id.stundenplan_freitag);
return rows;
}
主要布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/verplan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Karo"
tools:showIn="@layout/app_bar_main">
<ViewFlipper
android:id="@+id/ViewFlipp"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home Text" />
</RelativeLayout>
<LinearLayout
android:id="@+id/view2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="@+id/stundenplan_montag" layout="@layout/plantextviews"/>
<include android:id="@+id/stundenplan_dienstag" layout="@layout/plantextviews" android:layout_width="40dp" android:layout_height="wrap_content" />
<include android:id="@+id/stundenplan_mittwoch" layout="@layout/plantextviews" android:layout_width="40dp" android:layout_height="wrap_content" />
<include android:id="@+id/stundenplan_donnerstag" layout="@layout/plantextviews" android:layout_width="40dp" android:layout_height="wrap_content"/>
<include android:id="@+id/stundenplan_freitag" layout="@layout/plantextviews" android:layout_width="40dp" android:layout_height="wrap_content" />
</LinearLayout>
</ViewFlipper>
plantextviews布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column1" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column2" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column3" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column4" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column5" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column6" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column8" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column9" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column10" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/column11" />
答案 0 :(得分:2)
问题在于您要通过id = R.id.column1
查找11次视图,但是您应该找到包含ID R.id.column1
,R.id.column2
,R.id.column3
等的视图:
...
for(int i = 0; i < getRows().size(); i++) {
LinearLayout ll = (LinearLayout) Karo.main.findViewById(getRows().get(i));
TextView column1 = (TextView) ll.findViewById(R.id.column1);
TextView column2 = (TextView) ll.findViewById(R.id.column2);
TextView column3 = (TextView) ll.findViewById(R.id.column3);
...
...
column1.setText(stundenList.get(i).split(";")[0]);
column2.setText(stundenList.get(i).split(";")[1]);
column3.setText(stundenList.get(i).split(";")[2]);
...
...
}