0

I have a static class and I want to set a ListAdapter through the AsyncTask onPostExecute but it gives the error : nestedObject cannot be resolved to a type

static class(nested class) :

    public static class DummySectionFragment extends ListFragment
    {
    public PostData[] listData;
    public PostItemAdapter adapter;
    //DownloadWebPageTask task = new DownloadWebPageTask();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        this.generateDummyData();
        adapter = new PostItemAdapter(inflater.getContext(), android.R.layout.simple_list_item_1, listData);
        setListAdapter(adapter);
        //task.execute();
        View rootView = inflater.inflate(R.layout.fragment_section_news, container, false);
        return rootView;
    }

    private void generateDummyData()
    {
        PostData data = null;
        listData = new PostData[10];
        // sets a default value to Date,Title and URL
        for (int i = 0; i < 10; i++)
        {
            data = new PostData();
            data.postDate = "May 20, 2013";
            data.postTitle = "Post " + (i + 1) + " Title: This is the Post Title from RSS Feed";
            data.postThumbUrl = null;
            listData[i] = data;
        }
    }
}

AsyncTask :

    public static class DownloadWebPageTask extends AsyncTask<List<String>, Void, List<String>>
{
    @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());
        }
        return titles;
    }

    @Override
    protected void onPostExecute(List<String> result)
    {
        MainActivity.DummySectionFragment nestedObject =  new MainActivity.DummySectionFragment();
        ArrayAdapter<String> adapter = new ArrayAdapter<String> (nestedObject.this,R.layout.fragment_section_nieuws, result);
        nestedObject.this.setListAdapter(adapter);
    }
}

I'm trying to do this in a ListFragment instead of a ListActivity

Community
  • 1
  • 1
Shishi
  • 601
  • 2
  • 8
  • 27

1 Answers1

1

For the setListAdapter() call, place nestedObject.this with just nestedObject.

The first form attempts to qualify the this pointer with a class name but nestedObject is an object reference, not a class name. You refer to other objects with the object reference and not with this.

For the ArrayAdapter constructor, pass in a valid Context. Since the class is a static inner class, it doesn't have access to the containing class. You'll need to pass in a Context such as your activity reference as an argument to the DownloadWebPageTask. For example:

public static class DownloadWebPageTask extends AsyncTask<List<String>, Void, List<String>> {
    private Context mContext;

    DownloadWebPageTask(Context context) {
        mContext = context;
    }

    // now use mContext where a context is needed

(Note that holding onto an Activity reference like this can lead to significant memory leaks. But let's fix your build problems first. There are also many other problems in your code - interactively fixing them all is beyond the scope what SO questions are for.)

laalto
  • 150,114
  • 66
  • 286
  • 303
  • When I remove this I get two new errors saying : The constructor ArrayAdapter(MainActivity.DummySectionFragment,int,List) is undefined – Shishi Feb 06 '14 at 08:45
  • The DownloadWebPageTask is inside the MainActivity so I did this : `ArrayAdapter adapter = new ArrayAdapter (MainActivity.this,R.layout.fragment_section_nieuws, result);` and now it throws : No enclosing instance of the type MainActivity is accessible in scope – Shishi Feb 06 '14 at 08:50
  • When I do this and add `mContext` where `MainActivity.this` is on the comment above I can no longer execute my task in the ListFragment – Shishi Feb 06 '14 at 09:00
  • As you can see I update my DummySectionFragment class and it now throws : The constructor MainActivity.DownloadWebPageTask() is undefined on line `DownloadWebPageTask task = new DownloadWebPageTask();` – Shishi Feb 06 '14 at 09:06
  • Since we added an explicit constructor with `Context` arg, you need to pass e.g. `MainActivity.this` as an arg there: `new DownloadWebPageTask(MainActivity.this)` – laalto Feb 06 '14 at 09:08