代码之家  ›  专栏  ›  技术社区  ›  Malaika Khan

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

  •  0
  • Malaika Khan  · 技术社区  · 10 年前

    我想展示 ArrayList 属于 JSON 格式 TableLayout . 表格布局 是动态的,用于 onPost 方法 AsyncTask .

    如何从中获取数据 阵列列表 进入 表格布局 . 这是我使用 表格布局 .

    希望显示 阵列列表 在里面 表格布局 , 阵列列表 名称为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);
            }
        }
    
    
    }
    
    3 回复  |  直到 10 年前
        1
  •  2
  •   SANJAY GUPTA    10 年前

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

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

    使用这个post-exccute方法。我已经在这个PostExccute()方法中添加了这些行。

    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));
    
                    }
                }
    
        2
  •  1
  •   SANJAY GUPTA    10 年前

    在postExccute方法中使用这些行。

     itemsName = new TableRow(DailyMonthlyPerActivity.this);
                    tvItemName0 = new TextView(DailyMonthlyPerActivity.this);
                    tvItemName1 = new TextView(DailyMonthlyPerActivity.this);
                    tvItemName2 = new TextView(DailyMonthlyPerActivity.this);
    
        3
  •  0
  •   SANJAY GUPTA    10 年前

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