XML を解析し、その内容をテーブル レイアウトで表示しようとしています。行と列を動的に追加しています。テーブルを表示する代わりに実行すると、空白の画面が表示されます。
これは私のxmlファイルです。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CaptureActivity" >
<ScrollView
android:id="@+id/ScrollView_maintable"
android:layout_width="match_parent"
android:layout_height="400dp" >
<TableLayout
android:id="@+id/main_table"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1" >
</TableLayout>
これは私のアクティビティクラスです。
public class CaptureActivity extends Activity {
private String url = "http://192.168.3.140:8080/EmployeeXmlDemo/EmployeeList.xml";
//private String url = "http://192.168.2.166:8780/capture/clientRequest.do?r=employeeList&cid=0";
FetchEmployeeAsyncTask employeeAsyncTask = new FetchEmployeeAsyncTask(this);
private ArrayList<Employee> employees = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
employeeAsyncTask.execute(new String[] {url});
System.out.println("Status "+employeeAsyncTask.getStatus());
setContentView(R.layout.activity_capture);
}
@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_capture, menu);
return true;
}
public void showEmployees(ArrayList<Employee> employees) {
// TODO Auto-generated method stub
System.out.println("size is "+employees.size());
TableLayout employeeTable = (TableLayout) findViewById(R.id.main_table);
TableRow header = new TableRow(this);
header.setId(100);
//header.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView empCodeHeader = new TextView(this);
empCodeHeader.setId(200);
empCodeHeader.setText("Employee Code");
empCodeHeader.setTextSize(16);
empCodeHeader.setPadding(2,2,2,2);
empCodeHeader.setWidth(200);
header.addView(empCodeHeader);
TextView empNameHeader = new TextView(this);
empNameHeader.setId(201);
empNameHeader.setText("Employee Name");
empNameHeader.setTextSize(16);
empNameHeader.setPadding(2,2,2,2);
empNameHeader.setWidth(300);
header.addView(empNameHeader);
employeeTable.addView(header);//, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
for(int i=0; i<employees.size();i++){
Employee employee = (Employee) employees.get(i);
int count = 0;
TableRow empData = new TableRow(this);
empData.setId(300+count);
/*empData.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT ));*/
//tr.setClickable(true);
final TextView empCode = new TextView(this);
empCode.setId(300+count);
empCode.setText(employee.getCode());
empCode.setTextSize(16);
empCode.setPadding(2,2, 2, 2);
empData.addView(empCode);
final TextView empName = new TextView(this);
empName.setId(300+count);
empName.setText(employee.getName());
empName.setTextSize(16);
empName.setPadding(2, 2, 2, 2);
empData.addView(empName);
employeeTable.addView(empData); /*, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));*/
count++;
System.out.println("Name "+employee.getName());
System.out.println("Designatio "+employee.getDesignation());
}
setContentView(R.layout.activity_capture);
}
/**
* @return the employees
*/
public ArrayList<Employee> getEmployees() {
return employees;
}
/**
* @param employees the employees to set
*/
public void setEmployees(ArrayList<Employee> employees) {
this.employees = employees;
}
}
これは私の AsyncTask クラスです。
public class FetchEmployeeAsyncTask extends AsyncTask<String, Void, ArrayList<Employee> > {
private CaptureActivity activity;
public FetchEmployeeAsyncTask(CaptureActivity nextActivity) {
this.activity = nextActivity;
}
@Override
protected ArrayList<Employee> doInBackground(String... url) {
// TODO Auto-generated methoVoidd stub
ArrayList<Employee> employees = null;
for(String employeeUrl : url){
employees = fetch(employeeUrl);
}
return employees;
}
private ArrayList<Employee> fetch(String url) {
// TODO Auto-generated method stub
ArrayList<Employee> employees = null;
String response = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
employees = EmployeeXMLParser.XMLfromString(response);
System.out.println("Size in fetch "+employees.size());
//System.out.println("Employee Name :: " + employees.get(0).getFirstName() + " " + employees.get(0).getLastName());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} /*catch (XmlPullParserException e) {
// TODO Auto-generated catch block
System.out.println("Error parsing the response :: " + response);
e.printStackTrace();
}*/
return employees;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
public void onPostExecute(ArrayList<Employee> employees){
super.onPostExecute(employees);
System.out.println("in post execxute "+employees.size());
activity.showEmployees(employees);
//activity.setContentView(R.layout.activity_capture);
}
}