LeeJelin / android-section-list

Automatically exported from code.google.com/p/android-section-list
Other
0 stars 0 forks source link

How to/best way to reload/refresh #9

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi! and thank you for a great library! I works great, even for an android 
newbie like me. This may be an obvious question, but your library is so 
complete, I bet you've even thought of it, so please bear with me...

I'm trying out your sectionlist in a small hobby project of mine, where I 
download data from a json service I've made, that gets updated relatively often 
- several times a day.

I've started out with your:

public class SectionListActivity extends Activity 

and I've added an asynctask that goes something like this:

    private class SectionDownloader extends AsyncTask<String, Integer, Integer>
    {
        protected void onPreExecute(){
            //Setup is done here
        }

        protected Integer doInBackground(String... params) {
                   //populate json array much like your SectionListItem[] exampleArray
                   return Integer length of exampleArray
               } 

        protected void onPostExecute(Integer json_lngth)
        {
            if (json_lngth != null && exampleArray != null)
            {
                arrayAdapter = new StandardArrayAdapter(getApplicationContext(), R.id.example_text_view,
                        exampleArray);
                sectionAdapter = new SectionListAdapter(getLayoutInflater(),
                        arrayAdapter);

                listView = (SectionListView) findViewById(getResources().getIdentifier(
                        "section_list_view", "id",
                        this_package_name));
                listView.setAdapter(sectionAdapter);
            }
            else
            {
                // no network error
            }
        }
        protected void onProgressUpdate(Integer... params){
            //Update a progress bar here, or ignore it, it's up to you
        }

        protected void onCancelled(){
        }

    }

And it works great!

What I am wondering is how to use your option menu to implement a refresh 
button to force a reload of the json array, ie to go out and fetch new data 
from the server and update the list.

I've read about adapter.notifyDataSetChanged() and similar things, but it seems 
to me i have to call the async task first, but when I do that I get an 
exveption like:

08-17 23:21:08.251: E/AndroidRuntime(295): java.lang.NullPointerException
08-17 23:21:08.251: E/AndroidRuntime(295):  at 
pl.polidea.sectionedlist.SectionListAdapter.updateSessionCache(SectionListAdapte
r.java:75)

So as far as I can see something "wrong" happens in your updateSessionCache 
when I do it this way.

What would you suggest the proper way to reload data and update the sectionlist?

Thanks in advance, and sorry if this is a totally inappropriate question...

Original issue reported on code.google.com by ti...@netscape.net on 17 Aug 2012 at 9:55

GoogleCodeExporter commented 9 years ago
I think you need to do it through the underlying array adapter. You should not 
try to reset the adapter in the async task but rather reuse already existing 
array adapter, clear it's content, fill it with data again and then call 
"notifyDataSetChanged" on it. It will then in turn call notifyDataSetChanged of 
the section list adapter which will refresh in turn the list view which uses 
it. This seems to be most straightforward way of doing it.

Original comment by ja...@potiuk.com on 18 Aug 2012 at 9:45

GoogleCodeExporter commented 9 years ago
Thank you for your prompt reply!

You're solution is exactly what I ended up doing. Fortunately the problem ended 
up being my own:

My json data is grouped by date, with the dates being used as the section 
headers. Since my data is first an array dates with each date having an array 
of data for that date, I wrote a sorting and counting method that returns a 
single array with all my data. I also defined a property with the total count 
of array elements. So although I nulled out the array adapter, I forgot to 
reset the counter property that I used to initiate the exampleArray. The 
nullPointerExeption I was receiving was caused by the counter growing when i 
tried to refresh the list, since I iniated a much larger exampleArray than 
there was actual data.

A stupid mistake, all my own, confirming that your code works perfectly.

Again, thank you!

Original comment by ti...@netscape.net on 18 Aug 2012 at 11:54