我在我的 tableView 中显示 th JSON 结果时遇到问题,其中不是在插入获取的数据之前清除 tablerows,它只是将旧数据中的新数据连接起来,我不知道为什么。这是我的代码:
首先,我有一个 JSON 解析器,它根据所选城市和日期获取所有信息,我认为不需要显示该部分,但我只需将数据添加到列表数组中,然后执行此部分。
public void showinfo(){
// Get the TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViews();
Log.v(KahelTag, "eventsCleared");
// Go through each item in the array
for (int current = 0; current < infoID.size(); current++)
{
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//CREATE TEXT VIEW FOR INFO1
final TextView info1 = new TextView(this);
clubName.setId(200+current);
clubName.setText(info1.get(current));
clubName.setTextColor(Color.WHITE);
clubName.setTextSize(12);
clubName.setTypeface(null, Typeface.BOLD);
clubName.setTag(infoID.get(current));
clubName.setGravity(Gravity.CENTER_VERTICAL);
clubName.setLayoutParams(new LayoutParams(
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 65, getResources().getDisplayMetrics()),
LayoutParams.WRAP_CONTENT));
tr.addView(info1);
//CREATE TEXT VIEW FOR info2
TextView info2 = new TextView(this);
eventName.setId(current);
eventName.setText(info2.get(current));
eventName.setGravity(Gravity.CENTER_VERTICAL);
eventName.setTextSize(12);
eventName.setTextColor(Color.WHITE);
eventName.setLayoutParams(new LayoutParams(
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics()),
LayoutParams.WRAP_CONTENT));
tr.addView(info2);
//CREATE TEXT VIEW FOR INFO3
TextView info3 = new TextView(this);
eventDate.setId(current);
eventDate.setText(info3.get(current));
eventDate.setGravity(Gravity.CENTER_VERTICAL);
eventDate.setTextSize(12);
eventDate.setTextColor(Color.WHITE);
eventDate.setLayoutParams(new LayoutParams(
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics()),
LayoutParams.WRAP_CONTENT));
tr.addView(info3);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
现在我在使用 showinfo(); 设置内容视图后显示该信息;现在是微调器部分。它只是再次调用 JSON 代码来更改参数,然后再次在表格中显示信息。但它不像我想要的那样工作,因为正如我上面所说的,它只是连接数据。顺便说一句,我有两个为我的 JSON 文本传递的参数。这是微调器的代码:
Spinner spinParam1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinParam2 = (Spinner) findViewById(R.id.spinner2);
spinParam1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Object specify_param1 = parentView.getItemAtPosition(position);
if(specify_param1.toString() != "All"){
getEvents(specify_param1.toString(),null);
showinfo();
Log.v(KahelTag, "showinfo");
}
else
{
getEvents(null,null);
showinfo();
Log.v(KahelTag, "elseshowinfo");
}
}
public void onNothingSelected(AdapterView<?> parentView) {
getEvents(null,null);
showinfo();
Log.v(KahelTag, "nothingshowinfo");
}
});
spinParam2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Object specify_param2 = parentView.getItemAtPosition(position);
if(specify_param2.toString() != "Any"){
getEvents(specify_param2.toString(),null);
showinfo();
Log.v(KahelTag, "showinfo");
}
else
{
getEvents(null,null);
showinfo();
Log.v(KahelTag, "elseshowinfo");
}
}
public void onNothingSelected(AdapterView<?> parentView) {
getEvents(null,null);
showinfo();
Log.v(KahelTag, "nothingshowinfo");
}
});
我需要进行任何修改才能按我想要的方式工作吗?谢谢!