Php 如何在Android Textview上打印循环中获取的JSON数据
我被困在这里,我想用Textview在Android中打印值。但我只收到最后一行。 这里的响应是我正在解析的JSON编码的数组名,所有声明的字符串都是获得的数据的属性,这些数据将使用线性布局打印在4 textview中。我使用PHP从数据库中获取数据Php 如何在Android Textview上打印循环中获取的JSON数据,php,android,json,textview,Php,Android,Json,Textview,我被困在这里,我想用Textview在Android中打印值。但我只收到最后一行。 这里的响应是我正在解析的JSON编码的数组名,所有声明的字符串都是获得的数据的属性,这些数据将使用线性布局打印在4 textview中。我使用PHP从数据库中获取数据 TextView textView1 = (TextView)findViewById(R.id.ID); TextView textView2 = (TextView)findViewById(R.id.Name); TextView
TextView textView1 = (TextView)findViewById(R.id.ID);
TextView textView2 = (TextView)findViewById(R.id.Name);
TextView textView3 = (TextView)findViewById(R.id.Department);
TextView textView4 = (TextView)findViewById(R.id.Semester);
jsonArray = jsonObject.getJSONArray("response");
String ID, Name, Department, Semester;
int i = 0;
while (i < jsonArray.length()) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
ID = jsonObject1.getString("ID");
textView1.setText(ID);
Name = jsonObject1.getString("Name");
textView2.setText(Name);
Department = jsonObject1.getString("Department");
textView3.setText(Department);
Semester = jsonObject1.getString("Semester");
textView4.setText(Semester);
i++;
}
TextView textView1=(TextView)findViewById(R.id.id);
TextView textView2=(TextView)findViewById(R.id.Name);
TextView textView3=(TextView)findViewById(R.id.Department);
TextView textView4=(TextView)findViewById(R.id.sement);
jsonArray=jsonObject.getJSONArray(“响应”);
字符串ID、名称、部门、学期;
int i=0;
而(i
我有五行(元组)的数据,我想打印新行中的每一行。所以,在所有五行中应该有4个属性值。有没有办法不用存储数据就能做到这一点
编辑:我必须打印所有5个元组(行)的4个属性。意味着我总共需要(4 X 5=)20个文本视图。您是否尝试过PDO fetchAll方法。此方法主要根据数组格式的查询获取数据库中的所有行。如果我理解正确,您希望有4x5=20个文本视图 如果您批准,则必须使用listview或recyclerview来实现此目的
问题在于每次迭代都会覆盖
文本视图的内容
您应该尝试将其附加到当前文本中,例如:
textview.setText(textview.getText()+“some string”)
这里的问题是在textview上循环jsonArray。在每次迭代中,textView都会得到一个值,在每次新的迭代中,textView的上一个值都会被下一个第i个索引的jsonArray的新值更改。您可以创建TextView列表或创建recycler视图,以便插入和打印每个新的jsonArray值
这里只有4个文本视图,但要打印5个人。如果您希望它们以以下格式附加到这4个文本视图中:
身份证号码:123
姓名:约翰·迈克·佩森3
系:数学、计算机与工程
学期:123
你可以:
jsonArray = jsonObject.getJSONArray("response");
String ID, Name, Department, Semester;
int i = 0;
while (i < jsonArray.length()) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
ID = jsonObject1.getString("ID");
textView1.setText(textView1.getText().toString() + " "+ ID);
Name = jsonObject1.getString("Name");
textView2.setText(textView2.getText().toString() + " "+ Name);
Department = jsonObject1.getString("Department");
textView3.setText(textView3.getText().toString() + " "+ Department);
Semester = jsonObject1.getString("Semester");
textView4.setText(textView4.getText().toString() + " "+ Semester);
i++;
}
但是,如果您想要一个单独显示每个人的列表,我建议使用:
在res/layout中创建名为list_item.xml的文件:
谢谢你这么慷慨。我搜索了这个问题,找到了一个使用TableLayout的解决方案,这个解决方案对我来说更好更容易。下面是我使用的代码
我的XML代码是:
<HorizontalScrollView
android:id="@+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
我的JAVA代码就是在这里创建TableRow,Textview填充数据并添加数据
TableLayout tableLayout = (TableLayout)findViewById(R.id.table);
jsonArray = jsonObject.getJSONArray("response");
String ID, Name, Department, Semester;
int i = 0;
while (i < jsonArray.length()) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
ID = jsonObject1.getString("ID");
Name = jsonObject1.getString("Name");
Department = jsonObject1.getString("Department");
Semester = jsonObject1.getString("Semester");
i++;
TableRow tableRow1 = new TableRow(this);
TextView tv1 = new TextView(this);
tv1.setText(ID);
tv1.setTextColor(Color.RED);
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(20.0f);
tableRow1.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(Name);
tv2.setTextColor(Color.WHITE);
tv2.setGravity(Gravity.CENTER);
tv2.setTextSize(20.0f);
tableRow1.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(Department);
tv3.setTextColor(Color.WHITE);
tv3.setGravity(Gravity.CENTER);
tv3.setTextSize(20.0f);
tableRow1.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText(Semester);
tv4.setTextColor(Color.WHITE);
tv4.setGravity(Gravity.CENTER);
tv4.setTextSize(20.0f);
tableRow1.addView(tv4);
tableLayout.addView(tableRow1);
}
TableLayout TableLayout=(TableLayout)findviewbyd(R.id.table);
jsonArray=jsonObject.getJSONArray(“响应”);
字符串ID、名称、部门、学期;
int i=0;
而(i
TableLayout tableLayout = (TableLayout)findViewById(R.id.table);
jsonArray = jsonObject.getJSONArray("response");
String ID, Name, Department, Semester;
int i = 0;
while (i < jsonArray.length()) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
ID = jsonObject1.getString("ID");
Name = jsonObject1.getString("Name");
Department = jsonObject1.getString("Department");
Semester = jsonObject1.getString("Semester");
i++;
TableRow tableRow1 = new TableRow(this);
TextView tv1 = new TextView(this);
tv1.setText(ID);
tv1.setTextColor(Color.RED);
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(20.0f);
tableRow1.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(Name);
tv2.setTextColor(Color.WHITE);
tv2.setGravity(Gravity.CENTER);
tv2.setTextSize(20.0f);
tableRow1.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(Department);
tv3.setTextColor(Color.WHITE);
tv3.setGravity(Gravity.CENTER);
tv3.setTextSize(20.0f);
tableRow1.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText(Semester);
tv4.setTextColor(Color.WHITE);
tv4.setGravity(Gravity.CENTER);
tv4.setTextSize(20.0f);
tableRow1.addView(tv4);
tableLayout.addView(tableRow1);
}