I am receiving a warning when trying to execute a method inside a Fragment.
public class PrimaryFragmentDormir extends Fragment {
// Declare Variables
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapter adapter;
private List<WorldPopulation> worldpopulationlist = null;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.primary_layout_dormir,null);
// Get the view from listview_main.xml
// setContentView(R.layout.listview_main);
// Execute RemoteDataTask AsyncTask
issue here==> new RemoteDataTask.execute();
//test commit dell
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("Parse.com Custom ListView Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// Create the array
worldpopulationlist = new ArrayList<WorldPopulation>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Country");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("ranknum");
ob = query.find();
for (ParseObject country : ob) {
// Locate images in flag column
ParseFile image = (ParseFile) country.get("flag");
WorldPopulation map = new WorldPopulation();
map.setRank((String) country.get("rank"));
map.setCountry((String) country.get("country"));
map.setPopulation((String) country.get("population"));
map.setFlag(image.getUrl());
worldpopulationlist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) getView().findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(PrimaryFragmentDormir.this.getActivity(),
worldpopulationlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
I have searched for a solution, but I am not able to solve the issue.
Thank you
Is it me or your code cannot compile because the line
new RemoteDataTask.execute();
cannot be reached ? It's right after a return.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.primary_layout_dormir,null);
// ...
new RemoteDataTask.execute();
}
Secondly, as it has been mentioned, you need parenthesis to create a new object :
new RemoteDataTask().execute();
Try something like this :
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
new RemoteDataTask().execute();
return inflater.inflate(R.layout.primary_layout_dormir,null);
}
Your syntax is a little off here:
new RemoteDataTask.execute();
When you instantiate an object with new, you need parenthesis like so: new RemoteDataTask().
Your error says "cannot resolve symbol 'execute'", because the execute() method doesn't exist as a static method on the RemoteDataTask class, and even if it did it wouldn't work with the new operator.
Thus you want to change that line to:
new RemoteDataTask().execute();
Related
I have a ListView that has a SwipeRefreshLayout implemented on it. What I am trying to do is update the ListView when the user swipes down. I've tried looking for different options but unfortunately I am unable to find a solution.
Would it be better to reload the Fragment or the onCreate function?
Here is my code
public class NewsTab extends Fragment implements OnRefreshListener{
final LinkedList<News> listnews = new LinkedList<News>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_tab, container, false);
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
newslist = (ListView) view.findViewById(R.id.displaynews);
populatelist(listnews);
adapter = new CustomAdapter(getActivity(),listnews);
newslist.setAdapter(adapter);
return view;
}
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
new StartNewsLoad().execute("");
}
private class StartNewsLoad extends AsyncTask<String, Void, String> {
//ProgressDialog pd = null;
protected String doInBackground(String... params) {
reloadNews();
loadTopNews();
//NewsTab.listnews = new LinkedList<News>();
return null;
}
protected void onPostExecute(String result) {
adapter = new CustomAdapter(getActivity(),listnews);
adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
}
///ANSWER
I found the answer! I added the following line of code inside my costume adapter and called it outside as adapter.refresh();
public void refresh(LinkedList<News> newsList)
{
this.results = newsList;
notifyDataSetChanged();
}
Thank you everybody for your help!
First of all no need to create the adapter again in onPostExecute() as you have already initialized it in onCreateView().You simple need to notify the adapter of the changes in the "listnews" if it has changed.Make sure that your list actually changes before calling the notifyDataSetChanged().
Also please use debugger or log to check whether your onRefresh() is called or not.From the way i see, it should be overriden with #override annotation as it is the method of the interface implemented by your fragment(swipeRefreshLayout.setOnRefreshListener(this);).It should be like:
#Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
new StartNewsLoad().execute("");
}
In onCreateView() intialize your listview and swiperefereshLayout with listener and set your adapter to your listview like that --->
if(adapter==null&& listNews==null){
listnews=new ArrayList<object>();
adapter=new CustomAdapter(getActivity(),listnews);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();}
else{
listView.setAdapter(adapter);}
this way it also handle retaining the listData onOrientationchange.
and when you get api response or referesh listview with swipe layout,dont create new adapter just add listNews to your adapter and call adapter.notifyDataSetChanged();
I'm trying to parse a RSS feed and display it to the user via a Listview which is in a Fragment. The problem is that nothing shows up on the list and I get a few errors:
V/Error Parsing Data: java.io.IOException: Couldn't open http://feeds.bbci.co.uk/news/england/rss.xml
W/FragmentManager: moveToState: Fragment state for Thingy{3633e4e #1 id=0x7f0c0069} not updated inline; expected state 3 found 2
My Fragments class:
public class Thingy extends Fragment {
ListView mList;
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
adapter = new ArrayAdapter<>(getActivity(), R.layout.basic_list_item);
mList = ((ListView) v.findViewById(R.id.listView));
new GetRssFeed().execute("http://feeds.bbci.co.uk/news/england/rss.xml");
return v;
}
private class GetRssFeed extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
adapter.add("Pizza Steak");
try {
RssReader rssReader = new RssReader(params[0]);
for (RssItem item : rssReader.getItems()) {
adapter.add(item.getTitle());
}
} catch (Exception e) {
Log.v("Error Parsing Data", e + "");
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
mList.setAdapter(adapter);
}
}
}
Please add the permission on the manifest
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>
The problem may be in the URL that you are using where it isn't a valid RSS feed url,
so that, try to run your code by the this url: http://www.aljazeera.com/xml/rss/all.xml (for testing only).
I have a listview inside a fragment showing weather data(image,temperature and weather description). I fetch the data from here
When I rotate the emulator though I get a crash.:(. Here is my code. I use the setRetainedInstance but with out any result. Somehow I need to save and restore the asynchronous data I get,but I don't know how.
public class ForecastFragment extends Fragment{
ListView listView;
List<WeatherForecastData> WeatherForecastDataList;
String IMG_URL = "http://api.openweathermap.org/img/w/";
Fragment fragment;
public ForecastFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflate xml view and convert it to a View object
View rootView = inflater.inflate(R.layout.fragment_forecast, container, false);
//Initialise ListView.
listView = (ListView) rootView.findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = WeatherForecastDataList.get(position).getWeatherTemperature();
Toast.makeText(getActivity(),temp+" Have a nice day",Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
//Now we are ready for further processing
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
requestData("http://api.openweathermap.org/data/2.5/forecast/daily?lat=50.09&lon=14.42&cnt=9&&units=metric&mode=json");
}
//We create a MyTask object,and execute the async. thread with the specified url which is shown just above.
private void requestData(String uri) {
MyTask task = new MyTask();
task.execute(uri);
}
//AsyncTask that will do the asynchronous threading. It displays the weather's icon,description
//and temperature in the main thread via the OnPostExecute(...) method.
private class MyTask extends AsyncTask<String, String, List<WeatherForecastData>> {
#Override
protected void onPreExecute() {
//Used to initialise Views such as Progress Bars which are not needed for this
//project.
}
#Override
protected List<WeatherForecastData> doInBackground(String... params) {
//Read the url,specify the METHOD GET, and store it in content.
String content = HttpManager.getData(params[0]);
//JSON parsing of the openweather api's response. It is not hard,but I had to use the
//debugger quite a lot to make sure that I deserialise the correct JSON values into Strings.
WeatherForecastDataList = WeatherJSONParser.parseFeed(content);
//Fetching the url image
for (WeatherForecastData d : WeatherForecastDataList) {
try {
String imageUrl = IMG_URL +d.getPhoto();
InputStream in = (InputStream) new URL(imageUrl).getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
//Is it deprecated?
d.setBitmap(bitmap);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return WeatherForecastDataList;
}
//WeatherForecastData is the Object that contains all that instances we want to display.
#Override
protected void onPostExecute(List<WeatherForecastData> result) {
if (result == null) {
Toast.makeText(getActivity(), "There is some wrong,and data can not be displayed", Toast.LENGTH_LONG).show();
return;
}
WeatherForecastDataList = result;
//Display the ListView.
WeatherAdapter adapter = new WeatherAdapter(getActivity(), R.layout.weather_row,WeatherForecastDataList);
listView.setAdapter(adapter);
}
}
}
Here is my logcat.
08-01 19:45:59.857 21260-21260/com.theotziomakas.weatherapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
at com.theotziomakas.weatherapp.Fragments.WeatherAdapter.<init>(WeatherAdapter.java:34)
at com.theotziomakas.weatherapp.Fragments.ForecastFragment$MyTask.onPostExecute(ForecastFragment.java:144)
at com.theotziomakas.weatherapp.Fragments.ForecastFragment$MyTask.onPostExecute(ForecastFragment.java:98)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
You need to implement onSaveInstanceState to save the data, then retrieve it in onCreateView
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("YourVariableName", yourVariable);
super.onSaveInstanceState(savedInstanceState);
}
Then in onCreateView
if (savedInstanceState != null) {
yourVariable = savedInstanceState.getString("YourVariableName");
}
Check out the documentation for more info
Basically the flow is like the following: when a fragment is created (or when the user swipes to refresh the layout), an AsyncTask is executed. The ASyncTask retrieves the info from a URL and setup an adapter which is then assigned to a ListView.
This is the code of the Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tasks_page, container, false);
mSwipeRefreshLayout = (SwipeRefreshLayout)
rootView.findViewById(R.id.list_container);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light, android.R.color.holo_red_light);
lv = (ListView) rootView.findViewById(android.R.id.list);
getTasks();
return rootView;
}
private void getTasks() {
new TasksRetriever(mSwipeRefreshLayout,tasksAdapter,lv).execute();
}
#Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getTasks();
}
}, 1000);
}
The TaskRetriever constructor is:
public TasksRetriever(SwipeRefreshLayout srl, TasksAdapter ta, ListView lv) {
adpt = ta;
this.srl = srl;
this.lv = lv;
}
and postExecute is:
#Override
protected void onPostExecute(List<TasksItem> result) {
super.onPostExecute(result);
dialog.dismiss();
if (adpt == null) adpt = new TasksAdapter(result);
else adpt.setItemList(result);
lv.setAdapter(adpt);
adpt.notifyDataSetChanged();
srl.setRefreshing(false);
}
Not sure yet if it works but I was wondering if I'm on the right track because it doesn't look clean to me. On the other hand, I can't create the adapter and assign it to the ListView until I actually have data for it... Is this considering OK or am I doing it wrong?
I would set the list adapter up in the onCreate() method under your list view and make this a instance variable (not a local one) so that the Async task can access it as follows i.e.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lv = (ListView) rootView.findViewById(android.R.id.list);
mAdpt = new TasksAdapter(null);
lv.setAdapter(mAdpt);
}
And then in your onPostExecute()
#Override
protected void onPostExecute(List<TasksItem> result) {
dialog.dismiss();
mAdpt.clear();
for( TaskItem ti : result )
{
mAdpt.add( ti );
}
mAdpt.notifyDataSetChanged();
srl.setRefreshing(false);
}
Also make sure you account for the null value being passed into your adapter in the getView() method.
Hope this helps.
you CAN create an empty adapter and fill it later - in AsyncTask
your onPostExecute() should look like:
#Override
protected void onPostExecute(List<TasksItem> result) {
dialog.dismiss();
if (adpt == null){
adpt = new TasksAdapter(result);
lv.setAdapter(adpt);
}else{
adpt.clear();
}
for( TaskItem ti : result ) adpt.add( ti );
adpt.notifyDataSetChanged();
srl.setRefreshing(false);
}
I have used #chrisbanes ActionBar-PullToRefresh with my project, now I'm facing a conflict between ListView and PullToRefresh swipe down gesture.
When the ListView is already scrolled and I want to get to the top by swiping down, PullToRefresh trigger a refresh action instead of swiping the list up.
PS :
Here is my source code without extra stuff.
public class TestFragment extends Fragment implements OnRefreshListener {
private PullToRefreshLayout mPullToRefreshLayout;
private TestAdapter testAdapter;
private ListView testListView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.testActivity = (TestActivity)getActivity();
PullToRefreshLayout testLayout = (PullToRefreshLayout)inflater.inflate(R.layout.test_list_view, null);
this.testData = new ArrayList<Object>();
// Set List Adapter
this.testListView = new ListView(this.testActivity);
this.testData = getSearchResult();
this.testAdapter = new TestAdapter(this.testActivity, this.testData);
testListView.setAdapter(this.testAdapter);
testListView.setFocusableInTouchMode(false);
testLayout.addView(twitterListView);
return testLayout;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup viewGroup = (ViewGroup) view;
// As we're using a Fragment we create a PullToRefreshLayout manually
mPullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext());
// We can now setup the PullToRefreshLayout
ActionBarPullToRefresh.from(getActivity())
// We need to insert the PullToRefreshLayout into the Fragment's ViewGroup
.insertLayoutInto(viewGroup)
// all children are pullable
.allChildrenArePullable()
.listener(this)
.setup(mPullToRefreshLayout);
}
#Override
public void onRefreshStarted(View view) {
final TestAdapter adapter = this.testAdapter;
final TestActivity activity = this.testActivity;
final PullToRefreshLayout pull2Refresh = this.mPullToRefreshLayout;
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
/// refresh data source
getFragmentData(activity, true);
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// Notify Program Adapter that data has changed
adapter.notifyDataSetChanged();
}
});
// Notify PullToRefreshLayout that the refresh has finished
pull2Refresh.setRefreshComplete();
}
}.execute();
}
}
I found the issue
It was my mistake, first i must load ListView from Layout by identifier
-- this.testListView = new ListView(this.testActivity);
++ this.testListView = (ListView)testLayout.findViewById(R.id.test_listview);
And remove the last line
-- testLayout.addView(twitterListView);