在下面的代码中,我以表格的形式显示详细信息。现在,所有细节都来自JSON数组,并且细节以表格布局显示。
我想以同样的方式显示SI但该字段不在数据库中。
预期产出:
SI
1
2
3
|
|
| etc
根据我想显示SI号的患者数量。
更新回答
public class General_patient_list extends Activity {
String data = "";
TableLayout tl;
TableRow tr;
TextView si;
String queryString;
int numOfPatients = 0;
TextView[] snos;
TextView[] regnos;
TextView[] patNames;
TextView[] sex;
TextView[] regdates;
TextView[] adress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general_collection_report);
tl = (TableLayout) findViewById(R.id.maintable);
final String date1 = getIntent().getStringExtra("date1");
final String date2 = getIntent().getStringExtra("date2");
new Thread(new Runnable() {
public void run() {
queryString = "date1=" + date1 + "&date2="
+ date2;
data = DatabaseUtility.executeQueryPhp("general_registration",queryString);
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<Patients> patients = parseJSON(data);
numOfPatients = patients.size();
addData(patients);
}
});
}
}).start();
}
public ArrayList<Patients> parseJSON(String data) {
ArrayList<Patients> patients = new ArrayList<Patients>();
try {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Patients patient = new Patients();
//patient.setSi(json_data.getInt("si"));
patient.setRegno(json_data.getString("reg_no"));
patient.setPat_name(json_data.getString("name"));
patient.setSex(json_data.getString("sex"));
patient.setReg_date(json_data.getString("entry_date"));
patient.setAddress(json_data.getString("address"));
patients.add(patient);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return patients;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
si = new TextView(this);
si.setText("SI");
si.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
si.setPadding(5, 5, 5, 5);
si.setBackgroundColor(Color.parseColor("#rrggbb"));
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(si,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView reg_no = new TextView(this);
reg_no.setText("Reg_No");
reg_no.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
reg_no.setPadding(5, 5, 5, 5);
reg_no.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(reg_no,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView pat_name = new TextView(this);
pat_name.setText("Patient Name");
pat_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
pat_name.setPadding(5, 5, 5, 5);
pat_name.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(pat_name,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView sex = new TextView(this);
sex.setText("Sex");
sex.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
sex.setPadding(5, 5, 5, 5);
sex.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(sex,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView reg_date = new TextView(this);
reg_date.setText("Registration Date");
reg_date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
reg_date.setPadding(5, 5, 5, 5);
reg_date.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(reg_date,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView address = new TextView(this);
address.setText("Address");
address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
address.setPadding(5, 5, 5, 5);
address.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(address,params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
@SuppressWarnings({ "rawtypes", "deprecation" })
public void addData(ArrayList<Patients> patients) {
snos = new TextView[numOfPatients];
regnos = new TextView[numOfPatients];
patNames=new TextView[numOfPatients];
sex=new TextView[numOfPatients];
regdates=new TextView[numOfPatients];
adress=new TextView[numOfPatients];
int j = 0;
addHeader();
for (Iterator i = patients.iterator(); i.hasNext();) {
snos[j] = (TextView) findViewById(R.id.si);
regnos[j] = (TextView) findViewById(R.id.regno);
patNames[j]=(TextView)findViewById(R.id.patname);
sex[j]=(TextView)findViewById(R.id.sex);
regdates[j]=(TextView)findViewById(R.id.regdate);
adress[j]=(TextView)findViewById(R.id.adress);
Patients p = (Patients) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
if(j % 2==0){
tr.setBackgroundColor(Color.GREEN);
}
else{
tr.setBackgroundColor(Color.GRAY);
}
/** Creating a TextView to add to the row **/
snos[j] = new TextView(this);
int temp = j;
snos[j].setText(String.valueOf(temp +1));
snos[j].setId(j);
snos[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
snos[j].setPadding(5, 5, 5, 5);
// snos[j].setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.setPadding(10, 5, 5, 5);
Ll.addView(snos[j],params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
regnos[j] = new TextView(this);
regnos[j].setKeyListener(null);
regnos[j].setText(p.getRegno());
regnos[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
regnos[j].setPadding(5, 5, 5, 5);
// regnos[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(regnos[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
patNames[j] = new TextView(this);
patNames[j].setKeyListener(null);
patNames[j].setText(p.getPat_name());
patNames[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
patNames[j].setPadding(5, 5, 5, 5);
// patNames[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(patNames[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
sex[j] = new TextView(this);
sex[j].setKeyListener(null);
sex[j].setText(p.getSex());
sex[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
sex[j].setPadding(5, 5, 5, 5);
// sex[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(sex[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
regdates[j] = new TextView(this);
regdates[j].setKeyListener(null);
regdates[j].setText(p.getReg_date());
regdates[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
regdates[j].setPadding(5, 5, 5, 5);
// regdates[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(regdates[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
adress[j] = new TextView(this);
adress[j].setKeyListener(null);
adress[j].setText(p.getAddress());
adress[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
adress[j].setPadding(5, 5, 5, 5);
// adress[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(adress[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
j++;
}
}
答案 0 :(得分:0)
public class General_patient_list extends Activity {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;
String queryString;
int numOfPatients = 0;
TextView[] snos;
TextView[] regnos;
TextView[] patNames;
TextView[] sex;
TextView[] regdates;
TextView[] adress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general_collection_report);
tl = (TableLayout) findViewById(R.id.maintable);
final String date1 = getIntent().getStringExtra("date1");
final String date2 = getIntent().getStringExtra("date2");
new Thread(new Runnable() {
public void run() {
queryString = "date1=" + date1 + "&date2="
+ date2;
data = DatabaseUtility.executeQueryPhp("general_registration",queryString);
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<Patients> patients = parseJSON(data);
numOfPatients = patients.size();
addData(patients);
}
});
}
}).start();
}
public ArrayList<Patients> parseJSON(String data) {
ArrayList<Patients> patients = new ArrayList<Patients>();
try {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Patients patient = new Patients();
//patient.setSi(json_data.getInt("si"));
patient.setRegno(json_data.getString("reg_no"));
patient.setPat_name(json_data.getString("name"));
patient.setSex(json_data.getString("sex"));
patient.setReg_date(json_data.getString("entry_date"));
patient.setAddress(json_data.getString("address"));
patients.add(patient);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return patients;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
TextView si = new TextView(this);
si.setText("Si");
si.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
si.setPadding(5, 5, 5, 5);
si.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
Ll.setPadding(10, 5, 5, 5);
Ll.addView(si,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView reg_no = new TextView(this);
reg_no.setText("Reg_No");
reg_no.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
reg_no.setPadding(5, 5, 5, 5);
reg_no.setBackgroundColor(Color.RED);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(reg_no,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView pat_name = new TextView(this);
pat_name.setText("Patient Name");
pat_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
pat_name.setPadding(5, 5, 5, 5);
pat_name.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(pat_name,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView sex = new TextView(this);
sex.setText("Sex");
sex.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
sex.setPadding(5, 5, 5, 5);
sex.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(sex,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView reg_date = new TextView(this);
reg_date.setText("Registration Date");
reg_date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
reg_date.setPadding(5, 5, 5, 5);
reg_date.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(reg_date,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView address = new TextView(this);
address.setText("Address");
address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
address.setPadding(5, 5, 5, 5);
address.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(address,params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
@SuppressWarnings({ "rawtypes", "deprecation" })
public void addData(ArrayList<Patients> patients) {
snos = new TextView[numOfPatients];
regnos = new TextView[numOfPatients];
patNames=new TextView[numOfPatients];
sex=new TextView[numOfPatients];
regdates=new TextView[numOfPatients];
adress=new TextView[numOfPatients];
int j = 0;
addHeader();
for (Iterator i = patients.iterator(); i.hasNext();) {
//snos[j] = (TextView) findViewById(R.id.si);
//regnos[j] = (TextView) findViewById(R.id.regno);
//patNames[j]=(TextView)findViewById(R.id.patname);
//sex[j]=(TextView)findViewById(R.id.sex);
//regdates[j]=(TextView)findViewById(R.id.regdate);
//adress[j]=(TextView)findViewById(R.id.adress);
Patients p = (Patients) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
if(j % 2==0){
tr.setBackgroundColor(Color.BLUE);
}
else{
tr.setBackgroundColor(Color.GRAY);
}
/** Creating a TextView to add to the row **/
snos[j] = new TextView(this);
int temp = j;
snos[j].setText(String.valueOf(temp +1));
snos[j].setId(j);
snos[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
snos[j].setPadding(5, 5, 5, 5);
// snos[j].setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.setPadding(10, 5, 5, 5);
Ll.addView(snos[j],params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
regnos[j] = new TextView(this);
regnos[j].setKeyListener(null);
regnos[j].setText(p.getRegno());
regnos[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
regnos[j].setPadding(5, 5, 5, 5);
// regnos[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(regnos[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
patNames[j] = new TextView(this);
patNames[j].setKeyListener(null);
patNames[j].setText(p.getPat_name());
patNames[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
patNames[j].setPadding(5, 5, 5, 5);
// patNames[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(patNames[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
sex[j] = new TextView(this);
sex[j].setKeyListener(null);
sex[j].setText(p.getSex());
sex[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
sex[j].setPadding(5, 5, 5, 5);
// sex[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(sex[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
regdates[j] = new TextView(this);
regdates[j].setKeyListener(null);
regdates[j].setText(p.getReg_date());
regdates[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
regdates[j].setPadding(5, 5, 5, 5);
// regdates[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(regdates[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
adress[j] = new TextView(this);
adress[j].setKeyListener(null);
adress[j].setText(p.getAddress());
adress[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
adress[j].setPadding(5, 5, 5, 5);
// adress[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(adress[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
j++;
}
}
我不确定这是不是你想要的。我希望它有所帮助。