为什么当应该使用JSON数据填充时,arraylist仍然为空

时间:2014-07-02 21:51:47

标签: android json arraylist

我通过AsyncTask中的JSON数据填充项目对象并将其添加到ArrayList。我在onCreate活动方法中执行JSON类,之后我打印出ArrayList的大小为0.在JSON类onPostExecute方法中打印数据是好的,因此数据来自Web服务应该是这样。为什么在JSONParse执行后ArrayList仍然为空?

对不起,如果我忽略了一些基本的东西,我刚开始学习andorid。

public class StaggeredGridActivity extends Activity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private static final String TAG = "StaggeredGridActivity";
    public static final String SAVED_DATA_KEY = "SAVED_DATA";

    private StaggeredGridView mGridView;
    private boolean mHasRequestedMore;
    private SampleAdapter mAdapter;

    //JSON FIELDS SETUP

    //URL
    String url = "http://.../web_services/get_items_test.php";

    //JSON Node Names
    private static final String TAG_ARRAY = "items";
    private static final String TAG_ID = "id";
    private static final String TAG_ITEMNAME = "iname";
    private static final String TAG_NEWPRICE = "new_price";
    private static final String TAG_STORENAME = "store_name";
    private static final String TAG_CURR = "curr";

    //JSON ARRAY
    JSONArray items = null;

    //ITEMS LIST
    ArrayList<Item> item_list = new ArrayList<Item>();



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

        //Execute JSON
        new JSONParse().execute();

        //PRINTS OUT 0!
        System.out.println(item_list.size());
        setTitle("SGV");
        mGridView = (StaggeredGridView) findViewById(R.id.grid_view);

        //LayoutInflater layoutInflater = getLayoutInflater();

        //View header = layoutInflater.inflate(R.layout.list_item_header_footer, null);
        //View footer = layoutInflater.inflate(R.layout.list_item_header_footer, null);
        //TextView txtHeaderTitle = (TextView) header.findViewById(R.id.txt_title);
        //TextView txtFooterTitle =  (TextView) footer.findViewById(R.id.txt_title);
        //txtHeaderTitle.setText("THE HEADER!");
        //txtFooterTitle.setText("THE FOOTER!");

        //mGridView.addHeaderView(header);
        //mGridView.addFooterView(footer);
        mAdapter = new SampleAdapter(this, R.id.txt_line1, item_list);

        // do we have saved data?
        /*if (savedInstanceState != null) {
            mData = savedInstanceState.getStringArrayList(SAVED_DATA_KEY);
        }
        */
        //if (mData == null) {
        //    mData = SampleData.generateSampleData();
        //}
        //    
        //for (String data : mData) {
        //mAdapter.add(data);
        //}

        mGridView.setAdapter(mAdapter);

        /*mGridView.setOnScrollListener(this);
        mGridView.setOnItemClickListener(this);
        mGridView.setOnItemLongClickListener(this);*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_sgv_dynamic, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.col1:
                mGridView.setColumnCount(1);
                break;
            case R.id.col2:
                mGridView.setColumnCount(2);
                break;
            case R.id.col3:
                mGridView.setColumnCount(3);
                break;
        }
        return true;
    }

    /*@Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList(SAVED_DATA_KEY, item_list);
    }
   */
    @Override
    public void onScrollStateChanged(final AbsListView view, final int scrollState) {
        Log.d(TAG, "onScrollStateChanged:" + scrollState);
    }

    @Override
    public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {
        Log.d(TAG, "onScroll firstVisibleItem:" + firstVisibleItem +
                            " visibleItemCount:" + visibleItemCount +
                            " totalItemCount:" + totalItemCount);
        // our handling
        if (!mHasRequestedMore) {
            int lastInScreen = firstVisibleItem + visibleItemCount;
            if (lastInScreen >= totalItemCount) {
                Log.d(TAG, "onScroll lastInScreen - so load more");
                mHasRequestedMore = true;
               // onLoadMoreItems();
            }
        }
    }
    /*private void onLoadMoreItems() {
        final ArrayList<Item> item = JSONParse.execute();
        for (String data : sampleData) {
            mAdapter.add(data);
        }
        // stash all the data in our backing store
        mData.addAll(sampleData);
        // notify the adapter that we can update now
        mAdapter.notifyDataSetChanged();
        mHasRequestedMore = false;
    }*/

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
    {
        Toast.makeText(this, "Item Long Clicked: " + position, Toast.LENGTH_SHORT).show();
        return true;
    }

    /*****************************************JSON********************************************/
    //GET JSON DATA FROM URL 
    private class JSONParse extends AsyncTask<String, String, JSONObject> {

        JSONparser jParser = new JSONparser();
        private JSONObject json;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected JSONObject doInBackground(String... args) {

            // Getting JSON from URL
            json = jParser.getJSONFromUrl(url);
            return json;

        }

        @Override
        public void onPostExecute(JSONObject json) {

            try {
                 // Getting JSON Array from URL
                 items = json.getJSONArray(TAG_ARRAY);

                 for(int i = 0; i < items.length(); i++){

                     JSONObject c = items.getJSONObject(i);

                     // Storing  JSON item in a Variable
                     String id = c.getString(TAG_ID);
                     String itemname = c.getString(TAG_ITEMNAME);
                     String newprice = c.getString(TAG_NEWPRICE);
                     String storename = c.getString(TAG_STORENAME);
                     String curr = c.getString(TAG_CURR);

                     Item item = new Item();

                     item.setItemId(id);
                     item.setItemName(itemname);
                     item.setItemPrice(newprice);
                     item.setStoreName(storename);
                     item.setItemCurrency(curr);

                     item_list.add(item);

                     //This is good! It prints out all item names
                     System.out.println(item_list.get(i).getItemName());

                }

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


        }
    }
} 

3 个答案:

答案 0 :(得分:1)

在行new JSONParse.execute()之后获取onCreate中的所有代码并将其放入函数中。在onPostExecute()中调用该函数。

答案 1 :(得分:1)

new JSONParse().execute(); - 在后台完成,因此当您在System.out.println(item_list.size());方法中执行oncreate时,数据仍未下载...使用onPostExecute更新您的视图已经下载了数据...即加载适配器...您需要了解为什么要在AsyncTask中执行此操作。

达到此效果的目的,

 @Override
            public void onPostExecute(JSONObject json) {

                try {
                     // Getting JSON Array from URL
                     items = json.getJSONArray(TAG_ARRAY);

                     for(int i = 0; i < items.length(); i++){

                         JSONObject c = items.getJSONObject(i);

                         // Storing  JSON item in a Variable
                         String id = c.getString(TAG_ID);
                         String itemname = c.getString(TAG_ITEMNAME);
                         String newprice = c.getString(TAG_NEWPRICE);
                         String storename = c.getString(TAG_STORENAME);
                         String curr = c.getString(TAG_CURR);

                         Item item = new Item();

                         item.setItemId(id);
                         item.setItemName(itemname);
                         item.setItemPrice(newprice);
                         item.setStoreName(storename);
                         item.setItemCurrency(curr);

                         item_list.add(item);

                         //This is good! It prints out all item names
                         System.out.println(item_list.get(i).getItemName());
                    }

                     mAdapter = new SampleAdapter(this, R.id.txt_line1, item_list);

                     for (Item data : item_list) {
                         mAdapter.add(data);
                     }

                     mGridView.setAdapter(mAdapter);

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

答案 2 :(得分:0)

查询仍在执行,打印出来时结果尚未就绪。 拥有AsyncTask的onPostExecute()方法。在那里,您确定查询执行已完成,并且可以调用包含onCreate()方法中所有代码的函数。