I am trying to retrieve some image URLs and image texts by scraping a website when my app starts up. I then want to use arrays I've created of imageTexts and imageURLs to create a ViewPager so I can display the images as a scrollable gallery.
I execute the network task of fetching the webpage and parsing the HTML as an AsyncTask (downloadTask() in the code below).
My problem is that as this happens in the background it doesn't have time to fetch and parse the data before the code tries to display the images. I get a null pointer error when the code hits
new ViewPagerAdapter(this, getimageText(), getimageURLS())
I can deduce that the array that should eventually store the image URLs and the image Texts hasn't been created yet. This is at the point my app crashes.
Here's my code. I'm new to both Android and Java so clearly I could do with a lot of help!
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
setContentView(R.layout.viewpager_main);
// Fetch the list of URLs using a background network task
new downloadTask().execute(BASE_URL);
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(this, getimageText(), getimageURLS());
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
}
You can initially set a default image and you can modify your asyncTask to take in a callback method. This callback method would be a method that initializes your ViewPager. In your AsyncTask you would get the images and override the onPostExecute() method to call that callback method.
Example
class PictureGrabber extends AsyncTask<URL, VOID, VOID>{
IPicCallBack callBack;
PictureGrabber(IPicCallBack callback)
{
this.callback = callback;
}
protected Void doInBackGround(URL... urls){
}
protected void onPostExecute(VOID result)
{
callback.Init();
}
public interface IPicCallBack{
public void Init();
}
Then in your code you need to implement that IPicCallback and set it on the asyncTask and when the task is complete it'll call that method
Related
Here is the scenario that I am trying to resolve. I have a Layout which contains some data to be submitted to a server, this Activity layout needs to upload multiple files (Attachments) which are listed using RecyclerView.Adapter. In each attachment that is listed in bottom part of my layout is a progress bar that shows status or the percentage of the file that has been uploaded to the sever. On the click of the submit button, it calls an AsyncTask and in the onPostExecute method of this Activity layout I want to upload my attachments.
What is the best way to overcome this issue? Please help.
If your async task is an inner class of an activity or fragment, you can simply call a method that lives in the activity or fragment from within onPostExecute
class MyActivity extends Activity
{
public void showOrHideProgress(Result result)
{
// your code here
}
class MyAsyncTask extends AsyncTask
{
private final WeakReference><View> progressBarReference;
public MyAsyncTask(#NonNull View progress)
{
this.progressBarReference = new WeakReference<>(progress);
}
#Override
protected void onPostExecute(Result result)
{
super.onPostExecute(result);
showOrHideProgress(result); // or do something with progressBarReference
}
}
}
I have a fragment dialog containing a list of all the apps installed on the device.
Sometimes the loading takes some time and I would like to show a ProgressDialog while it loads.
I've tried the following code which didn't do any good :
#Override
public void onAttach(Activity activity) {
// Show progress dialog
showProgressDialog(activity);
super.onAttach(activity);
}
private void showProgressDialog(Activity activity) {
mProgressDialog = new ProgressDialog(activity);
mProgressDialog.setTitle(R.string.loading);
mProgressDialog.setMessage(getString(R.string.shared_options_wait_while_applist_loads));
mProgressDialog.show();
}
The onCreate loads the whole list and the app images, and then I use :
#Override
public void onStart() {
stopProgressDialog();
super.onStart();
}
private void stopProgressDialog() {
mProgressDialog.dismiss();
}
Now I'm thinking about loading the whole list in a async task, but I can't figure what should the async task do, it should probably load the list, but how can I wait for the list and get the list when it's ready ? (I believe something like a callback should be used?)
Thanks
You can watch dataset changes in your adapter by adding a DataSetObserver.
#Override
public void onCreate(Bundle savedInstanceSate){
super.onCreate(savedInstanceState);
setContentview(R.layout.main_view);
ListView myList = (ListView) findViewById(R.id.my_list);
MyDataTypeAdapter adapter = new MyDataTypeAdapter();
myList.setAdapter(adapter);
adapter.registerDataSetObserver( new DataSetObserver(){
#Override
public void onChange(){
//do stuff here
}
});
}
This way onChange will be loaded when your dataSet changes and you can download your image there.
However, I will better do an AsyncTask for each row in your adapter and download it's image independently. You could also use UniversalImageLoader library for this purpose.
you can try Android Bitmap Fun example
Android Image Demo : http://developer.android.com/training/displaying-bitmaps/index.html
This example with GridView, you can use the same adapter for ListView by changing view in listItem.
I have 3 Tabs like in the android development tutorial
Now what I want to do is very simple I use Fragments on each page. I want to show different content from a rss feed on each page.
The problem is when I go to the next tab it runs AsyncTask (which is in onCreateView) of the previous Fragment.
So you start on Page 1 it loads the content fine. Then when you go to Page 2 is runs the onCreateView of the Fragment of Page 1 again. And obviously gives an NullException. The point is it should not be running AsyncTask of Page 1 at all at that Page 2.
I don't think there is any example code needed if so tell me which part you need to see. Then I will edit my question.
AsyncTask inside a ListFragment :
public class MyAsyncTask extends AsyncTask<List<String>, Void, List<String>>
{
// List of messages of the rss feed
private List<Message> messages;
private volatile boolean running = true;
#SuppressWarnings("unused")
private WeakReference<NieuwsSectionFragment> fragmentWeakRef;
private MyAsyncTask(NieuwsSectionFragment fragment)
{
this.fragmentWeakRef = new WeakReference<NieuwsSectionFragment>(fragment);
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
mProgress.show();
// progress.setVisibility(View.VISIBLE); //<< set here
}
#Override
protected void onCancelled()
{
Log.w("onCancelled", "now cancelled");
running = false;
}
#Override
protected List<String> doInBackground(List<String>... urls)
{
FeedParser parser = FeedParserFactory.getParser();
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages)
{
titles.add(msg.getTitle());
// Log.w("doInBackground", msg.getTitle());
}
return titles;
}
#Override
protected void onPostExecute(List<String> result)
{
super.onPostExecute(result);
mProgress.dismiss();
if (result != null)
{
PostData data = null;
listData = new PostData[result.size()];
for (int i = 0; i < result.size(); i++)
{
data = new PostData();
data.postTitle = result.get(i);
data.postThumbUrl = "http://igo.nl/foto/app_thumb/28991-Taxi-vast-na-poging-tot-nemen-van-sluiproute.jpg";
listData[i] = data;
Log.w("onPostExecute", "" + listData[i].postTitle);
}
adapter = new PostItemAdapter (getActivity(), android.R.layout.simple_list_item_1, listData);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
It's called inside a method and that method is executed inside the onCreateView of the ListFragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
startNewAsyncTask();
View rootView = inflater.inflate(R.layout.fragment_section_nieuws, container, false);
return rootView;
}
#SuppressWarnings("unchecked")
public void startNewAsyncTask()
{
MyAsyncTask asyncTask = new MyAsyncTask(this);
this.asyncTaskWeakRef = new WeakReference<MyAsyncTask>(asyncTask);
asyncTask.execute();
}
The LogCat :
Try using isAdded() before onPostExecute(). isAdded() returns true if the fragment is currently added to its activity.
http://developer.android.com/reference/android/app/Fragment.html#isAdded()
#Override
protected void postExecute(){
if(isAdded()){
//perform Display Changes
}
}
Move your
startNewAsyncTask();
to onActivityCreated()
I'm assuming your using FragmentPagerAdapter with your ViewPager.
To enable smooth animations, the ViewPager by default keeps the current fragment and the two neighbors in resumed state. So onCreateView is not the best place to start the AsyncTask.
Instead you need to create a custom listener interface. The fragments in the ViewPager should implement it, and call the new interface from the ViewPager's OnPageChangeListener.
Check out my answer to this question or you can read the whole tutorial here.
You're getting that exception because you're calling getActivity() too early. You should do it after onActivityCreated() (see this diagram)
Executing of onCreateView() in background is fine and actually is default behaviour. The thing is, ViewPager is optimised to load a content of neighbour non-visible pages in background to improve UX. You can do this:
mViewPager.setOffscreenPageLimit(2); (default value is 1) to load all 3 pages at once (1 is loading as currently visible and other 2 as optimisation). Or set it to 0 to disable this behaviour, but it's not the best idea.
In general, you should cash your loaded data and do not load it again by making your fragment's lifecycle methods as light as possible. Page limit of 2 is fine for 3 pages, but if you'll have for example 10 pages, limit of 9 is too much.
If I've understood your question right, I think you need unique content with each Fragment right?
Try using the varible arguments of the execute method. For example:
yourTask.execute(<some-unique-URL>, parameter, one-more-parameter);
In this way you can pass a unique URL per fragment form which you can get your content.
I feel you already have this. The doInBackground method has the List of URLs. You just need to pass that information in the execute method and utilize it in doInBackground.
Hope this helps!
It is normal that it runs the AsyncTask from the adjacent Fragments, since the ViewPager + PagerAdapter combo, works loading the current, previous and next Fragment.
You should focus the problem not to stop AsyncTask from running, but to let it run w/o throwing a NullPointerException.
The following should be called inside onCreateView()
adapter = new PostItemAdapter (getActivity(), android.R.layout.simple_list_item_1, myList);
setListAdapter(adapter);
And then, onPostExecute()
myList.clear();
myList.addAll(listData);
adapter.notifyDataSetChanged();
The ViewPager will create views for fragments in adjacent pages and destroy views for fragments which are not adjacent to current page. Thus, the onCreateView of page 1 will get called if you navigate from page1->page2->page3->page2. You can have the viewpager keep more pages in memory by using ViewPager.setOffscreenPageLimit.
The fragmentPagerAdapter retains the fragment objects. Only the views are destroyed. Thus, when viewpage recreates page1's view, the fragment object is the same. Hence, all the fields in the fragment will get retained.
As in most applications where there's no realtime data, it is not required not load the data every time the view of the fragment is created, you can store the data in the fragment object after loading. Then, before starting the AsyncTask in onCreateView/onActivityCreated, check if the data has been previously loaded or not.
class PageFragment {
private List<String> mData;
...
void onActivityCreated() {
if (data == null) { // OR if the data is expired
startAsyncTask();
} else {
updateViews();
}
}
void updateViews() {
// Display mData in views
}
class LoadDataTask extends AsyncTask<List<String>, ..., ...> {
...
void onPostExecute(List<String> result) {
PageFragment.this.mData = result;
PageFragment.this.updateViews();
}
}
I recommend that you use loaders for loading data for a fragment. For your purpose, you can configure a loader to load data only once.
This is a great tutorial on Loaders.
http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html
In the tutorial, the loader is configured to return previous data immediately if available, and then fetch data in background and return it after fetching completes. Thus, the UI will get updated after fresh data gets downloaded but at the same time, it will show the previous data initially while the download happens.
You can use another activity - this activity will run asynctask and then move to your fragment related activity. In this way it should call only once.
In case you need to update Fragment UI using this AsyncTask then use a static method to call through AsyncTask.
I have an app that downloads data which must be displayed within the fragments of a viewpager. I dont know how to call the ListFragment adaptername.notifyDataSetChanged() in the AsyncTask that does the data download.
For example say i am downloading the temperature forecast for the next seven days:
my app has the following structure:
MainActivity: Starts an AsyncTask to download the data in onCreate() and gives the user choice (button) of which day to look at. Clicking the button launches SecondActivity and passes the day index to the ViewPager (to set the current view).
SecondActivity: Contains a ViewPager that contains 7 of the same ListFragments (The list display the temperature over a period of 5 hours, so the list has 5 entries).
MyListFragment: when this loads it sets the adapter to display each temperature (If the data is downloaded) otherwise it sets the temperature to "loading..."
Now my problem is, if the user waits on the MainActivity until the data downloads they can then proceed to the ViewPager to see the ListFragment temperatures without problem. But if they try click a day and load the ViewPager before the download completes the fragments will forever just say "loading..."
I need a way that I can reload the adapter within the ListFragment from the onPostExecute() of my AsyncTask in MainActivity. To do this though i need to be able to actually access the ListFragment that the ViewPager is displaying. How do update the adapter onPostExecute()?
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
...
new LoadData().execute();
}
protected class LoadData extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
//Download happens here
}
protected void onPostExecute(String result) {
//I need to tell the viewpager in SecondActivity to reload the ListFragment it is currently showing here
}
}
SecondActivity
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private int[] temperatureArray;
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = getIntent();
int[] defaultTemps = {0, 0, 0, 0, 0};
temperatureArray = getIntArrayExtra("temps", defaultTemps);
mPager = (ViewPager) findViewById(R.id.pagerID);
mPager.setCurrentItem(intent.getIntExtra("page", 0));
mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
...
public Fragment getItem(int position) {
MyListFragment lf = new MyListFragment();
lf.setTemperatures(temperatureArray);
return lf;
}
}
I need a way to be able to refresh the current displayed fragment when i finish downloading in the AsyncTask.
I can suggest doing one of two things, not sure if these are best practices.
You can Have the AsyncTask send a broadcast with a unique action when it has finished loading the information. That would of course have to be done from OnPostExecute:
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (result != null) {
//_Do whatever action you normally do, like storing result to database.
//fire up the broadcast
Intent intent = new Intent(Home.ACTION_FEED_LOADING_FINISHED);
mContext.sendBroadcast(intent);
}
}
And then intercept that via a BroadcastReceiver on your Fragment's code.
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_FEED_LOADING_FINISHED)) {
//The AsyncTask Finished loading data
}
}
};
Once the event is received, since the BroadcastReceiver is in the Fragment, where the views are loaded, you can refresh your UI accordingly.
Or,
You can pass the View you want to refresh as a paremeter on your AsyncTask.
Imagine your AsyncTask class like this:
private static class getInternetStuffTask extends AsyncTask<String, Void, Void> {
Then you can create a custom constructor to receive the view as paremeter:
public getInternetStuffTask (final Context context, final ListView list) {
this.mContext = context;
this.mListView= list;
}
Then, during onPostExecute, when data is loaded, you can simply set the adapter to the ListView directly form the AsyncTask.
You're touching on two different problems. First, use a Service for downloading. Services are there for long running operations (like a download) that live outside an Activity's lifecycle.
Second, to communicate between Activities and Fragments you'll want to use an callback interface. The link provided is to the docs which do an excellent job of explaining and providing samples.
I have an activity which has to grab heavy images, resize them and loads into GridView. Since all this process of fetching, resizing is time consuming, when I try to load this screen from another ACtivity, I get a delay.
How can I first load the content of this "grid activity" and open it only after all images have been loaded?
Using startActivityForResult? If this is a solution, how can I detect that all images have been loaded since GridView load images on scroll.
EDIT
I was suggested to use AsyncTask which I used dozens of times before. But something confuses me.
Should I send custom adapter initialization to the AsyncTask?
mGridView = (GridView) findViewById(R.id.gridview);
mImageAdapter = new ImageAdapter(this);
mGridView.setAdapter(mImageAdapter);
Or I should do async task inside of the custom adapter, namely inside the method getView and with each gridview cell create a new call to my AsyncTask?
You can use the AsyncTask by putting all the code of fetching and resizing in the doInBacground method, you can save data in an object (may be an ArrayList of Bitmap) to be returned to the onPostExecute method (see AsyncTask genric types).
Then will also have to change your ImageAdapter constructor by adding the object you got from AsyncTask to it as a parameter.
public class DownloadPdfTask extends AsyncTask<Void, Integer, ArrayList<Bitmap>> {
private ArrayList<Bitmap> images;
#Override
protected ArrayList<Bitmap> doInBackground(Void... arg0) {
// TODO Auto-generated method stub
/** your long task here, and set "images" **/
return images;
}
#Override
protected void onPostExecute(ArrayList<Bitmap> images) {
// TODO Auto-generated method stub
super.onPostExecute(images);
mGridView = (GridView) findViewById(R.id.gridview);
mImageAdapter = new ImageAdapter(this, images); // new construcor
mGridView.setAdapter(mImageAdapter);
}
}
Use the images list as you want in your Adapter !
Try creating an AsyncTask.
Create your Activity normally. At the end of onCreate(), start your AsyncTask.
On your AsyncTask's onPreExecute() method, make some animation or message appear to the user.
On doInBackground() work on your images.
On onPostExecute(), load your images on the GridView and stop the animation or dismiss the message.
This way, your Activity won't have the delay you mentioned while being created.