I have a problem on displaying th JSON Result in my tableView wherein instead of clearing the tablerows before inserting the fetched data, it just concatinates the new data from the old ones and I can’t figure out why. Here’s my code:
First I have a JSON Parser which gets all of the information based on the selected city and dates and I think there is no need to show that part but I just add the data into List Array then do this part.
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));
}
}
Now I show that info after setting the content view with showinfo();
And now for the spinner part. It just call again the JSON codes to change parameters then Display the info out in the table again. But It doesn’t work as I wanted since as I said above it just concatinates the data. By the way, I have two parameters that is passed for my JSON text. Here’s the code for the spinner:
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");
}
});
Any revisions that I need to make for this to work the way I wanted? Thanks!
Haven’t noticed it but the main cause of the problem is the JSON String array where I store all of the data which is why it concatenates the data from the previous output. Solved the problem by clearing out the arrays first before parsing JSON string. 🙂