我在onPostExecute
.
我更新了代码
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class LogStatus extends Activity {
TableLayout tlLogStatus;
// url to make request
private static String url = "http://api.androidhive.info/contacts/";
// JSON Node names
private static final String TAG_LOGDETAILS = "contacts";
private static final String TAG_DATE = "name";
private static final String TAG_PERC = "id";
// contacts JSONArray
JSONArray jaLog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_status);
TableLayout tlLogStatus = (TableLayout) findViewById(R.id.main_table);
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.RED);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_date = new TextView(this);
label_date.setId(20);
label_date.setText("Date");
label_date.setTextColor(Color.WHITE);
label_date.setPadding(5, 5, 5, 5);
tr_head.addView(label_date);// add the column to the table row here
TextView label_perc = new TextView(this);
label_perc.setId(21);// define id that must be unique
label_perc.setText("Percentage"); // set the text for the header
label_perc.setTextColor(Color.WHITE); // set the color
label_perc.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_perc); // add the column to the table row here
tlLogStatus.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
new MyAsyncTask().execute(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_log_status, menu);
return true;
}
private class MyAsyncTask extends AsyncTask<String, Integer, JSONObject>{
@Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(params[0]);
return json;
}
protected void onPostExecute(JSONObject json){
//pb.setVisibility(View.GONE);
try {
// Getting Array of Contacts
jaLog = json.getJSONArray(TAG_LOGDETAILS);
TableLayout tlLogStatus = (TableLayout) findViewById(R.id.main_table);
Integer count=0;
// looping through All Contacts
for(int i = 0; i < jaLog.length(); i++){
JSONObject c = jaLog.getJSONObject(i);
// Storing each json item in variable
String date = c.getString(TAG_DATE);
String perc = c.getString(TAG_PERC);
// Create the table row
TableRow tr = new TableRow(LogStatus.this);
if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
if(count%2==0) tr.setBackgroundColor(Color.BLACK);
tr.setId(100+count);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//Create two columns to add as table data
// Create a TextView to add date
TextView labelDATE = new TextView(LogStatus.this);
labelDATE.setId(200+count);
labelDATE.setText(date);
labelDATE.setPadding(2, 0, 5, 0);
labelDATE.setTextColor(Color.WHITE);
tr.addView(labelDATE);
TextView labelWEIGHT = new TextView(LogStatus.this);
labelWEIGHT.setId(200+count);
labelWEIGHT.setText(perc);
labelWEIGHT.setTextColor(Color.WHITE);
tr.addView(labelWEIGHT);
// finally add this to the table row
tlLogStatus.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress){
//pb.setProgress(progress[0]);
}
}
}
我在这里遇到问题:
tlLogStatus.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
我不知道如何解决这个问题。请帮忙
谢谢..