如何在 TableLayout 中显示数据 在 A syncTask 的方法之后



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

如何将数据从ArrayList获取到TableLayout。这是我正在使用TableLayout的活动。

想要在TableLayout中显示ArrayListArrayList名称为联系人列表。

  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);
        }
    }

}

好的。现在试试这个。从创建中删除这些行

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

我在这个PostExcecute()方法中添加了这些行。

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));
                }
            }

在postExcecut方法中使用这些行。

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

在你的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));
                }
            }

相关内容

最新更新