0

我有一个带有两个列表视图的活动,它们应该从服务器的mysql数据库中填充我使用两个异步任务在后台加载列表视图中的数据,它显示从服务器加载但是当它完成列表视图中没有显示任何内容时,我曾经“扩展活动”而不是ListActivity

这是代码请帮我解决这个问题

    public class HomeActivity extends Activity implements OnClickListener {

    // Progress Dialog
    private ListAdapter adapter,adapter2;
    private ProgressDialog pDialog,pDialog2;
    Button btnallshops,btnorderarchive,btnmyprofile;
    ListView listMyShop,listMyOrder;
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    JSONParser jParser2 = new JSONParser();

    ArrayList<HashMap<String, String>> MyShopsList;
    ArrayList<HashMap<String, String>> MyOrdersList;
    // url to get all paint shops list
    // link e url_all_paintshops bayad ba link e monaseb jaigozari
    // shavad*************************
    private static String url_all_myshops = "http://aaa/androidphp/get_myfavorite_shops.php";
    private static String url_all_myorders = "http://aaa/androidphp/get_myorders.php";
    // JSON Node
    // names************************IMPORTANT*****************************
    private static final String TAG_SUCCESS = "success";

    // naam e marboot be khorooji r JSON az file e
    // php********************************
    private static final String TAG_ALLPAINTSHOPS = "all";
    private static final String TAG_ALLORDERS = "all";

    // field haye marboot be TABLE e morede nazar ra dar inja
    // misazim************************
    private static final String TAG_PAINTSHOP_NAME = "name";
    private static final String TAG_ADDRESS = "address";

    private static final String TAG_PICTURE = "Picture";
    private static final String TAG_STORENAME = "storeName";
    private static final String TAG_STATE = "State";
    private static final String TAG_DELIVERYADDRESS = "DeliveryAddress";
    private static final String TAG_READYDATETIME = "ReadyDateTime";
    private static final String TAG_ITEM = "Item";


    // products JSONArray
    JSONArray AllMyshops,AllMyOrders = null;


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

//      initial buttons
        btnallshops = (Button) findViewById(R.id.btnAllShops);
        btnorderarchive = (Button) findViewById(R.id.btnOrdersArchive);
        btnmyprofile = (Button) findViewById(R.id.btnMyprofile);

        btnallshops.setOnClickListener(this);
        btnorderarchive.setOnClickListener(this);
        btnmyprofile.setOnClickListener(this);

        MyShopsList = new ArrayList<HashMap<String, String>>();
        MyOrdersList = new ArrayList<HashMap<String, String>>();

        // Loading favorite paintshops in Background Thread
        new LoadAllMyshops().execute();
//      Loading orders in Background Thread
//      new LoadAllMyOrders().execute();

        // Get listview
//      ListView lv = getListView();
          listMyShop = (ListView) findViewById(R.id.listMyShops);

        // on seleting single Myshop************************************

          listMyShop.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });

          listMyOrder = (ListView) findViewById(R.id.listMyOrders);

        // on seleting single Myshop************************************

          listMyOrder.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });
    }

    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllMyshops extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(HomeActivity.this);
            pDialog.setMessage("Loading My shops. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("painter_id", "1")); 
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_myshops,
                    "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All My shops: ", json.toString());

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

                if (success == 1) {
                    // painshop/s found
                    // Getting Array of paintshops
                    AllMyshops = json.getJSONArray(TAG_ALLPAINTSHOPS);

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

                        // Storing each json item in variable
                        String name = c.getString(TAG_PAINTSHOP_NAME);
                        String address = c.getString(TAG_ADDRESS);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PAINTSHOP_NAME, name);
                        map.put(TAG_ADDRESS, address);

                        // adding HashList to ArrayList
                        MyShopsList.add(map);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            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
                     * */
                    adapter = new SimpleAdapter(HomeActivity.this, MyShopsList,
                            R.layout.list_row_my_shops, new String[] {
                                    TAG_PAINTSHOP_NAME, TAG_ADDRESS },
                            new int[] { R.id.tvPaintShop, R.id.tvAddress });
                    // updating listview
//                  setListAdapter(adapter);
                    listMyShop.setAdapter(adapter);

//                  Loading orders in Background Thread
                    new LoadAllMyOrders().execute();
                }
            });

        }

    }
//****************************************************************My ORDER******************************





/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllMyOrders extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog2 = new ProgressDialog(HomeActivity.this);
        pDialog2.setMessage("Loading My Orders. Please wait...");
        pDialog2.setIndeterminate(false);
        pDialog2.setCancelable(false);
        pDialog2.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params2 = new ArrayList<NameValuePair>();

        // "1" SHOMARE ID PAINTER HAST ... BADAN AZ PREFERENCE MIGIRE
        params2.add(new BasicNameValuePair("painter_id", "1")); 

        // getting JSON string from URL
        JSONObject json2 = jParser2.makeHttpRequest(url_all_myorders,
                "GET", params2);

        // Check your log cat for JSON reponse
        Log.d("All My orders: ", json2.toString());

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

            if (success == 1) {
                // painshop/s found
                // Getting Array of paintshops
                AllMyOrders = json2.getJSONArray(TAG_ALLORDERS);

                // looping through All Orders
                for (int i = 0; i < AllMyOrders.length(); i++) {
                    JSONObject c2 = AllMyOrders.getJSONObject(i);

                    // Storing each json2 item in variable
                    String Picture = c2.getString(TAG_PICTURE);
                    String storeName = c2.getString(TAG_STORENAME);
                    String State = c2.getString(TAG_STATE);
                    String DeliveryAddress = c2.getString(TAG_DELIVERYADDRESS);
                    String ReadyDateTime = c2.getString(TAG_READYDATETIME);
                    String Item = c2.getString(TAG_ITEM);


                    // creating new HashMap
                    HashMap<String, String> map2 = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map2.put(TAG_PICTURE, Picture);
                    map2.put(TAG_STORENAME, storeName);
                    map2.put(TAG_STATE, State);
                    map2.put(TAG_DELIVERYADDRESS, DeliveryAddress);
                    map2.put(TAG_READYDATETIME, ReadyDateTime);
                    map2.put(TAG_ITEM, Item);


                    // adding HashList to ArrayList
                    MyShopsList.add(map2);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog2.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                adapter2 = new SimpleAdapter(HomeActivity.this, MyOrdersList,
                        R.layout.list_row_my_order, new String[] {
                         TAG_STORENAME,TAG_STATE,TAG_DELIVERYADDRESS,TAG_READYDATETIME,TAG_ITEM },
                        new int[] { R.id.tvStoreName,R.id.tvState, R.id.tvDeliveryAddress,R.id.tvReadyDateTime, R.id.tvItem});
                // updating listview
//              setListAdapter(adapter2);
                listMyOrder.setAdapter(adapter2);
            }
        });

    }

}

这段代码我做错了什么???

提前致谢....

4

1 回答 1

0

我遇到的同样问题的解决方案......

您正在执行方法后更新您的适配器..但这对我来说也不是..

这样做 -->

1)上面的oncreate方法创建一个像这样的公共方法

public void this_is_the_public_method()
{
   //Place your array adapter code here....

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,R.layout.simplerow,list);
dataAdapter.setDropDownViewResource(R.layout.simplerow);
noof.setAdapter(dataAdapter);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

2)在代码行之后调用您创建的公共方法

new LoadAllMyshops().execute();
this_is_the_public_method(); // call your new method which sets the array adapter

3)注意:您的 LoadAllMyshops 的 OnPostExecute 方法您需要做的就是更新您的 ArrayList MyshopysList 或任何东西..

同样,您需要做的是

  • -> 创建一个用于设置数组适配器的公共方法
  • -> 调用你的异步任务 -> 在 postexecute 中更新你的数组列表
  • -> 到 yourasynctask().execute() 的下一行;调用你的公共方法

而已..

希望能帮助到你...

于 2013-10-08T22:45:10.277 回答