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
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.)