AsyncTask的post方法中如何在TableLayout中显示数据

How to show data in TableLayout in post Method of AsyncTask

我想在TableLayout中显示JSON格式的ArrayListTableLayout 是动态的,用在 AsyncTaskonPost 方法中。

如何从 ArrayList 获取数据到 TableLayout。 这是我的 activity,我在其中使用 TableLayout

想在 TableLayout 中显示 ArrayListArrayList 中的姓名为 ContactList。

  public class DailyMonthlyPerActivity extends Activity implements   OnClickListener {

/////...VARIABLES...//////////////////
String month,week;
Button btnShowRpt;
UserFunction userFunction;
//JSONObject json,json_user;
private ProgressDialog pDialog;
private static String url = "http://192.168.0.2/SOmething/Something/1" ;
TableLayout tl;
TableRow itemsName ;
TextView tvItemName0 ;
TextView tvItemName1 ;
TextView tvItemName2;


private static final String TAG_CONTACTS = "Address";
private static final String TAG_DATA = "Name";
private static final String TAG_ID = "Name";
private static final String TAG_NAME = "Name";
private static final String TAG_EMAIL = "Age";
private static final String TAG_ADDRESS = "Name";
private static final String TAG_GENDER = "Name";
private static final String TAG_PHONE = "Age";
private static final String TAG_PHONE_MOBILE = "Name";
private static final String TAG_PHONE_HOME = "Name";
private static final String TAG_PHONE_OFFICE = "Name";
ArrayList<HashMap<String, String>> contactList;
////////////////////////////


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_monthly_per);

    contactList= new ArrayList<HashMap<String,String>>();
     itemsName = new TableRow(this);
     tvItemName0 = new TextView(this);
     tvItemName1 = new TextView(this);
     tvItemName2 = new TextView(this);
    tl = (TableLayout)findViewById(R.id.tableLayoutLocationSaleandRecovery);
    btnShowRpt = (Button)findViewById(R.id.btnShowReports);
    btnShowRpt.setOnClickListener(this);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()== R.id.btnShowReports)
    {
        new GetContacts().execute();

    }

}
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(DailyMonthlyPerActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
         if (jsonStr != null) {

            try {
                JSONArray jsonObj = new JSONArray(jsonStr);

                // looping through All Contacts
                for (int i = 0; i < jsonObj.length(); i++) {
                    JSONObject c = jsonObj.getJSONObject(i);
                    String name = c.getString("Name");
                    String email = c.getString("Age");
                    String address = c.getString("Address");
                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    //contact.put(TAG_ID, id);
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_EMAIL, email);
                    contact.put(TAG_CONTACTS, address);
                    // adding contact to contact list
                    contactList.add(contact);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }   
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        itemsName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
        tl.addView(itemsName, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT));

        tvItemName0.setText(TAG_NAME);
        tvItemName1.setText(TAG_EMAIL);
        tvItemName2.setText(TAG_CONTACTS);
        itemsName.addView(tvItemName0);
        itemsName.addView(tvItemName1);
        itemsName.addView(tvItemName2);
        }
    }


}

在你的 postExecute() 方法中试试这个。

protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (pDialog.isShowing())
                    pDialog.dismiss();
                for (int i = 0; i < contactList.size(); i++) {
                    itemsName.setLayoutParams(new TableRow.LayoutParams(
                            TableRow.LayoutParams.WRAP_CONTENT,
                            TableRow.LayoutParams.MATCH_PARENT));
                    tvItemName0.setText(contactList.get(i).get(TAG_NAME));
                    tvItemName1.setText(contactList.get(i).get(TAG_EMAIL));
                    tvItemName2.setText(contactList.get(i).get(TAG_CONTACTS));
                    itemsName.addView(tvItemName0);
                    itemsName.addView(tvItemName1);
                    itemsName.addView(tvItemName2);
                    tl.addView(itemsName, new TableLayout.LayoutParams(
                            TableLayout.LayoutParams.WRAP_CONTENT,
                            TableLayout.LayoutParams.MATCH_PARENT));

                }
            }

Ok.Now 尝试 this.remove 来自 oncreate

的这些行
 itemsName = new TableRow(this);
 tvItemName0 = new TextView(this);
 tvItemName1 = new TextView(this);
 tvItemName2 = new TextView(this);

使用此 post 执行 Method.I 已将这些行添加到此 ONPostExecute() 方法中。

protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (pDialog.isShowing())
                    pDialog.dismiss();
                for (int i = 0; i < contactList.size(); i++) {
itemsName = new TableRow(this);
     tvItemName0 = new TextView(this);
     tvItemName1 = new TextView(this);
     tvItemName2 = new TextView(this);
                    itemsName.setLayoutParams(new TableRow.LayoutParams(
                            TableRow.LayoutParams.WRAP_CONTENT,
                            TableRow.LayoutParams.MATCH_PARENT));
                    tvItemName0.setText(contactList.get(i).get(TAG_NAME));
                    tvItemName1.setText(contactList.get(i).get(TAG_EMAIL));
                    tvItemName2.setText(contactList.get(i).get(TAG_CONTACTS));
                    itemsName.addView(tvItemName0);
                    itemsName.addView(tvItemName1);
                    itemsName.addView(tvItemName2);
                    tl.addView(itemsName, new TableLayout.LayoutParams(
                            TableLayout.LayoutParams.WRAP_CONTENT,
                            TableLayout.LayoutParams.MATCH_PARENT));

                }
            }

在您的 postExcecute 方法中使用这些行。

 itemsName = new TableRow(DailyMonthlyPerActivity.this);
                tvItemName0 = new TextView(DailyMonthlyPerActivity.this);
                tvItemName1 = new TextView(DailyMonthlyPerActivity.this);
                tvItemName2 = new TextView(DailyMonthlyPerActivity.this);