我被困在这里,我想在Android中使用Textview打印价值。但我只是最后一行。 这里的响应是我正在解析的JSON编码数组名称,所有声明的字符串都是获取数据的属性,使用Linear Layout在4 textview中打印。我正在使用PHP从数据库中获取数据。
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++;
}
我有五行(元组)数据,我想在新行中打印每一行。因此,在所有五行中应该有4个属性值。有没有办法在不存储数据的情况下这样做。
编辑:我必须为所有5个元组(行)打印4个属性。意味着我需要总共(4 X 5 =)20 textview。
答案 0 :(得分:1)
问题在于,每次迭代都会覆盖textviews
的内容。
您应该尝试将其附加到当前文本中,例如:
textview.setText(textview.getText() + "some string")
答案 1 :(得分:0)
你试过PDO fetchAll方法吗?该方法主要根据数组格式的查询从数据库中获取所有行。
答案 2 :(得分:0)
如果我理解正确,您希望有4x5 = 20个textviews。
如果您同意,则必须使用listview或recyclerview来实现此目的。
答案 3 :(得分:0)
这里的问题是你在文本视图上循环你的jsonArray。在每次迭代中,textView都会获得一个值,并且在每次新迭代中,textView的先前值都会被新的jsonArray值更改为下一个第i个索引。您可以创建textViews列表或创建回收站视图,以便插入和打印每个新的jsonArray值。
答案 4 :(得分:0)
此处只有4个TextView,但您想要打印5个人。如果您希望将它们以格式:
附加到这4个textView中ID:1 2 3 ...
姓名:John Mike Person3 ......
部门:数学CS工程..
学期:1 2 3 ..
你这样做: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++;
}
关键是获取现有文本:
TextView textview1 = (TextView) findViewById(R.id.abbreviated);
String newText = jsonObject1.getString("Your_Field")
String oldText = textview1.getText().toString();
textview1.setText(oldText+ " " +newText);
但如果您想要一个单独显示每个人的列表,我建议您使用 RecyclerView :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
>
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/department"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/semester"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
&#13;
在包中创建适配器作为MyAdapter.java:
公共类MyAdapter扩展了RecyclerView.Adapter { 私人Arraylist人;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView tv_id;
public TextView tv_name;
public TextView tv_department;
public TextView tv_semester;
public ViewHolder(View view) {
super(v);
textView1 = (TextView) itemView.findViewById(R.id.id);
textView2 = (TextView) itemView.findViewById(R.id.name);
textView3 = (TextView) itemView.findViewById(R.id.department);
textView4 = (TextView) itemView.findViewById(R.id.semester);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(Arraylist myPeople) {
people = myPeople;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new MyViewHolder(itemView);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Person person = people.get(position);
holder.id.setText(person.getID());
holder.name.setText(person.getName());
holder.department.setText(movie.getDepartment());
holder.semester.setText(movie.getSemester());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return people.length;
}
}
在MainActivity.java中设置recycleler视图
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
Arraylist<Person> people = new Arraylist<>();
//Store your json object into this arraylist of persons
//Create a Person Object
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
将此细分添加到activity_main.xml布局
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
&#13;
答案 5 :(得分:0)
感谢你这么慷慨。我搜索了这个问题,并找到了一个使用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);
}