如何将内部异步类列表发送到活动中?

时间:2015-07-01 13:00:44

标签: java arraylist android-asynctask return

所以我的班级FinalOrder根据名为LoadUserDetails的异步类中的电子邮件加载用户数据。

我需要从异步类中获取列表usersList,因为我需要使用数据。我尝试过使用界面但它没有用。

有人能告诉我我做错了什么吗? 有没有人有其他建议?

两个异步类都在FinalOrder活动中。

提前致谢

public class FinalOrder extends Activity implements AsyncResponse{

    HashMap<String, String> map;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    // url to get all products list
    private static String url_all_products = "http://10.0.2.2/android_login_api/include/getProducts.php";
    private static String url_insert_order = "http://10.0.2.2/android_login_api/include/insertOrder1.php";
    private static String url_all_users = "http://10.0.2.2/android_login_api/include/getUsers.php";

    JSONArray users = null;
    JSONArray products = null;


    TextView txtMesaj;
    static TextView txtSumaTotala;
    ListView listOrder;
    Button btnFinalizare, btnSterge;
    EditText etCantit;
    private SQLiteHandler db;


    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    //order
    private static final String TAG_IDC = "idc";
    private static final String TAG_NUMEP = "nume";
    private static final String TAG_CANTIT = "cantitate";
    private static final String TAG_SUBTOT = "subtotal";

    //user
    private static final String TAG_UTILIZATORI = "utilizatori";
    private static final String TAG_UID = "uid";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_NUMEC = "nume";
    private static final String TAG_PRENUME = "prenume";
    private static final String TAG_ADRESA = "adresa";
    private static final String TAG_ORAS = "oras";
    private static final String TAG_TELEFON = "telefon";

    //product
    private static final String TAG_PRODUCTS = "produse";
    private static final String TAG_PID = "idprod";
    private static final String TAG_NUME = "nume";

    public double total;
    HashMap<String, String> sum = new HashMap<String, String>();

    String email = "";

    private ProgressDialog pDialog;
    List<HashMap<String, String>> comanda;
    List<HashMap<String, String>> usersList;
    private List<HashMap<String, String>> userDetail;
    List<HashMap<String, String>> productsList;

    String uid = "";

    String categorie = "";
    MyOrderAdapter adapter;


    public List<HashMap<String, String>> getList()
    {
        return productsList;
    }

    public void setList(List<HashMap<String, String>> cmd)
    {
        productsList =  cmd;
    }

    public void setUser(List<HashMap<String, String>> us)
    {
        this.userDetail.addAll(us);
    }

    public List<HashMap<String, String>> getUser()
    {
        return userDetail ;
    }

    public List<HashMap<String, String>> getuss()
    {
        return usersList ;
    }

    @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_final_order);

            txtMesaj = (TextView)findViewById(R.id.txtMesaj);
            txtSumaTotala = (TextView)findViewById(R.id.txtSumaTotala);
            listOrder = (ListView)findViewById(R.id.listOrder);
            btnFinalizare = (Button)findViewById(R.id.btnFinalizare);

            db = new SQLiteHandler(getApplicationContext());
            comanda = new ArrayList<HashMap<String, String>>();
            productsList = new ArrayList<HashMap<String, String>>();
            usersList = new ArrayList<HashMap<String, String>>();
            userDetail = new ArrayList<HashMap<String, String>>();

            Intent intent = getIntent();
            email = intent.getStringExtra(TAG_EMAIL);

            sum = db.getSum();
            total = Double.parseDouble(sum.get("suma"));

            new LoadAllOrders().execute();

            btnFinalizare.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    // TODO Auto-generated method stub

                    //comanda minima de 25 lei
                    sum = db.getSum();
                    total = Double.parseDouble(sum.get("suma"));
                    if(total >= 25)
                    {
                    productsList = db.getOrders();

                    LoadUserDetails ld = new LoadUserDetails();
                    ld.execute();
                    ld.delegate = FinalOrder.this;


                    for(HashMap<String, String> mapp: userDetail)
                    if(mapp.get(TAG_NUME).equals(email)) //daca am gasit acelasi nume de produs, iau id-ul
                    {
                        String uid = mapp.get(TAG_UID);
                        String nume = mapp.get(TAG_NUMEC);
                        String prenume = mapp.get(TAG_PRENUME);

                        Log.d("Marinela", uid + " " + nume + " " + prenume);
                    }

                    for(HashMap<String, String> mappp:productsList)
                    {
                        String pid = mappp.get(TAG_PID);
                        String numep = mappp.get(TAG_NUME);
                        String cant = mappp.get(TAG_CANTIT);
                        String subtot = mappp.get(TAG_SUBTOT);

                        Log.d("Marinela", pid + " " + numep + " " + cant + " " + subtot);

                    }
                    //db.deleteOrders();
                    Toast.makeText(getApplicationContext(), "Nr com: " + String.valueOf(db.getRowCountOrder()), Toast.LENGTH_LONG).show();
                    }
                    else
                        Toast.makeText(getApplicationContext(), "Comanda minima este de 25 lei!", Toast.LENGTH_LONG).show();


                    }
            });
        }

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.final_order, menu);
            return true;

        }

    class LoadAllOrders extends AsyncTask<String, String, String> {


        @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(FinalOrder.this);
                pDialog.setMessage("Incarcare...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            runOnUiThread(new Runnable() {
                    public void run() {
                    sum = db.getSum();
                    total = Double.parseDouble(sum.get("suma"));
                    txtSumaTotala.setText("Suma finala: " + total);

                    comanda = db.getOrders();
                    setList(comanda);

                    Log.d("elem: ", comanda.toString());
                    }
                    });
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                    public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    String [] from = new String[]{TAG_IDC, TAG_NUMEP, TAG_CANTIT};
                    int [] to = new int[] {R.id.idc, R.id.txtNumeProd, R.id.etCantit};

                    adapter = new MyOrderAdapter( FinalOrder.this, R.layout.finalize_order, comanda);//, from, to);
                    // updating listview
                    listOrder.setAdapter(adapter);
                    ((MyOrderAdapter) listOrder.getAdapter()).notifyDataSetChanged();


                    adapter.setOnDataChangeListener(new MyOrderAdapter.OnDataChangeListener(){
                            public void onDataChanged(int size){
                            //update suma
                            sum = db.getSum();
                            total = Double.parseDouble(sum.get("suma"));
                            //txtSumaTotala.setText("Suma finala: " + adapter.getTotal());
                            }
                            });


                    //update suma
                    //sum = db.getSum();
                    //total = Double.parseDouble(sum.get("suma"));
                    //txtSumaTotala.setText("Suma finala: " + adapter.getTotal());


                    }
            });

        }
    }

    class LoadUserDetails extends AsyncTask<String, String, List<HashMap<String, String>>> {

        public AsyncResponse delegate=null;

        @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(FinalOrder.this);
                pDialog.setMessage("Incarcare...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

        protected List<HashMap<String, String>> doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair(TAG_EMAIL, email));

            // getting JSON string from URL

            JSONObject json = null;
            json = new JSONObject();
            json = jParser.makeHttpRequest(url_all_users, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("User details: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Users
                    users = json.getJSONArray(TAG_UTILIZATORI);

                    // looping through All Users
                    for (int i = 0; i < users.length(); i++) {
                        JSONObject c = users.getJSONObject(i);

                        // Storing each json item in variable
                        String uid = c.getString(TAG_UID);
                        //String email = c.getString(TAG_EMAIL);
                        String nume = c.getString(TAG_NUME);
                        String prenume = c.getString(TAG_PRENUME);
                        String adresa = c.getString(TAG_ADRESA);
                        String oras = c.getString(TAG_ORAS);
                        String telefon = c.getString(TAG_TELEFON);
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_UID, uid);
                        //map.put(TAG_EMAIL, email);
                        map.put(TAG_NUME, nume);
                        map.put(TAG_PRENUME, prenume);
                        map.put(TAG_ADRESA, adresa);
                        map.put(TAG_ORAS, oras);
                        map.put(TAG_TELEFON, telefon);
                        // adding HashList to ArrayList
                        usersList.add(map);

                    }
                    Log.d("Marinela", String.valueOf(usersList.size()) + "dib");
                    return usersList;

                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(List<HashMap<String, String>> result) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();

            delegate.processFinish(result);

            Log.d("Marinela", String.valueOf(result.size()) + "oper");

            Log.d("Marinela", String.valueOf(usersList.size()) + "ope");
            setUser(usersList);

            if(userDetail.size()==0|| userDetail.isEmpty() || userDetail==null)
                Log.d("Marinela",String.valueOf(getUser().size())+ " asa");
            else
                Log.d("Marinela","user");


        }
    }

    @Override
        public void processFinish(List<HashMap<String, String>> output) {
            // TODO Auto-generated method stub
            setUser(output);

        }
}

0 个答案:

没有答案