Android - Listviews in Fragments - android

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

Related

Updating listview on fragment

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();

AsyncTask Fragment Rotation

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

Having problems to implement a setOnClickListener - Fragment

I'm having problems to implement an onclick to the items I have in my list. the error is below:
06-14 16:15:43.861 11651-11651/wk.gon250.dublinbike E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: wk.gon250.dublinbike, PID: 11651
java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
at android.widget.AdapterView.setOnClickListener(AdapterView.java:778)
at tab.Tab2.onCreateView(Tab2.java:67)
The code of my class is:
public class Tab2 extends Fragment
{
static ListView listView;
static CustomAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_2, container, false);
String Url = "http://mypath";
RequestQueue queue = Volley.newRequestQueue(getActivity());
final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "Please, Wait", "Loading..");
listView = (ListView) v.findViewById(R.id.listViewT_stations);
JsonObjectRequest req = new JsonObjectRequest(Url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Gson gson = new Gson();
JSONObject jsonNetwork = new JSONObject(response.getString("network"));
JSONArray stationsArray = jsonNetwork.getJSONArray("stations");
StationJson[] stations = gson.fromJson(stationsArray.toString(), StationJson[].class);
adapter = new CustomAdapter(getActivity(), stations, R.layout.station_item);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
progressDialog.cancel();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Manage Error
progressDialog.cancel();
}
});
queue.add(req);
listView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return v;
} }
I can't no use a listFragment, so any sugestion? How can I implement a onclick to the items I have to open a new view? and what is the best practice?
Thanks!!
The error message says:
Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
The OnClickListener is meant for click events on whole views. In an AdapterView, you usually want to register click events for each item separately. For this you need to use OnItemClickListener.

ListView and PullToRefresh swipe down conflict

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

When i rotate the device (Android), the adapter variable receives null?

In the code below (shortened for clarity) when I rotate the device, the adapter variable receives null and gridView variable receives null.
Can anyone help me keep the adapter and gridView after rotating the device screen?
Thank.
(Sorry for bad english)
public class FlickrXmlFragment extends Fragment {
private GridView gridView;
private FlickrGridViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//...
View view = darkInflater.inflate(R.layout.fragment_flickr_xml,
container, false);
//...
gridView = (GridView) view.findViewById(R.id.flickr_gridView);
return view;
}
class FlickrPhotoSearchThread extends
AsyncTask<String, Void, List<FlickrImagem>> {
#Override
protected void onPreExecute() { //... process... }
#Override
protected List<FlickrImagem> doInBackground(String... params) { //process... }
#Override
protected void onPostExecute(List<FlickrImagem> result) {
//...
adapter = new FlickrGridViewAdapter(getActivity(), 0, result);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
//...
}
}
}
Handle the Orientation changes for your activity.
Please refer to this: http://developer.android.com/guide/topics/manifest/activity-element.html#config
Declare your activity like this:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">

Categories

Resources