У меня есть простой макет таблицы, который я хочу заполнить, просто инициализируя определенный тип
объекта в моем методе onCreate. Во-первых, это макет у меня
jobs_table.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/Jobs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="@+id/JobRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/JobID" android:layout_column="1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
<TextView android:id="@+id/JobStatus" android:layout_column="2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
<TextView android:id="@+id/Customer" android:layout_column="3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
<TextView android:id="@+id/Department" android:layout_column="4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
<TextView android:id="@+id/DocType" android:layout_column="5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
</TableRow>
</TableLayout>
И активность
package com.producermobile;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import com.producermobile.jobs.ProducerJobRows;
public class JobRowsActivity extends Activity {
private BatchingJobRows jobRows;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jobs_table);
jobRows = new BatchingJobRows(this);
}
}
Теперь идея заключается в том, что когда я создаю свой jobRows, данные для таблицы будут загружены объектом BatchingJobRows, который, как мы надеемся, затем будет доступен для моей деятельности. У меня нет способа проверить это в mo, хотя я не уверен, правильна ли эта концепция (мое главное беспокойство - цикл while, который добавляет строки в таблицу).
В любом случае, вот упрощенная версия того, что происходит в BatchingJobRows.
package com.producermobile.jobs;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.producermobile.R;
public class BatchingJobRows {
private TableLayout table;
private View tableView;
private Context context;
private HashMap<String,ArrayList<String>> jobs;
private ArrayList<String> jobsVals;
public BatchingJobRows(Context context) {
this.context = context;
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tableView = inflater.inflate(R.layout.jobs_table, null);
table = (TableLayout) tableView.findViewById(R.id.Jobs);
jobVals = new ArrayList<String>();
jobVals.add("HELD");
jobVals.add("DELL");
jobVals.add("PRINT");
jobVals.add("STATEMENT");
jobs = new HashMap<String,ArrayList<String>>();
jobs.put("012345",jobVals);
jobs.put("123456",jobVals);
this.setRows();
}
public void setRows() {
Iterator<String> keys = jobs.keySet().iterator();
while(keys.hasNext()) {
//Init Objects
TableRow row = (TableRow) TableView.findViewById(R.id.JobRow);
TextView jobid = (TextView) tableView.findViewById(R.id.JobID);
TextView jobstatus = (TextView) tableView.findViewById(R.id.JobStatus);
TextView customer = (TextView) tableView.findViewById(R.id.Customer);
TextView department = (TextView) tableView.findViewById(R.id.Department);
TextView doctype = (TextView) tableView.findViewById(R.id.DocType);
String key = keys.next();
jobid.setText(key);
jobstatus.setText(jobs.get(key).get(0));
customer.setText(jobs.get(key).get(1));
department.setText(jobs.get(key).get(2));
doctype.setText(jobs.get(key).get(3));
row.addView(jobid);
row.addView(jobstatus);
row.addView(customer);
row.addView(department);
row.addView(doctype);
table.addView(row);
}
}
}