Obtaining assets under

I am trying to parse an XML file in a stream inside a fragment.

A partial snippet of my code:

public void onCreate(Bundle savedInstanceState)
    {
  mAdapter = new ListItemNearbyStoresAdapter(getActivity().getApplicationContext(), mStoresByKey);

         setListAdapter(mAdapter);

         // Load the list of stores from hard coded xml
         loadStoresByThread(getActivity().getApplicationContext());

    }

    private void loadStoresByThread(final Context ctx)
        {
            Thread t = new Thread()
            {

                public void run()
                {

                    try
                    {


                        Log.d(TAG, "In the thread");
                        String[] files = ctx.getResources().getStringArray(R.array.storefiles);
                       // String[] files={"s1.xml"};



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

                            try
                            {
                                InputStream stream = getActivity().getAssets().open(files[i]);

                                 NearbyItemDomFeedParser parser = new NearbyItemDomFeedParser(stream);
                                ArrayList<Store> stores = parser.parse();
                                Log.e("no of fioles read","asd :"+stores.size());
                                mStores.addAll(stores);
                                cache.setItems(mStores);
                            }
                            catch (Exception e)
                            {
                                Log.d(TAG, "Exception caught" + e);
                            }
                        }
}
}

Note that this entire class extends the list fragment.

Is access to resources correct inside the fragment?

The reason I can not read the file.

There are no exceptions, so I can not determine the exact error.

But finally, after parsing using the builder, I get 0 elements.

Any suggestions?

+3
source share
1 answer

Is access to resources correct inside the fragment?

Use Activity( getActivity()), not Application( getApplicationContext()) and see if that helps. Unless you have specific instructions from someone who knows what they are talking about, never use getApplicationContext()in your application.

+14
source

All Articles