Reputation: 3952
I've an activity where the RSS feeds are loaded from a website and get displayed in a list view. The thing is it takes few seconds to load stuffs into listview. And i want to implement a progressbar to notify the user about the loading of data.
Below is the code of displaying the RSS feeds..
public class RSS extends ListActivity {
List<String> headlines;
List<String> links;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rss);
// Initializing instance variables
headlines = new ArrayList<String>();
links = new ArrayList<String>();
try {
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Binding data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}
private InputStream getInputStream(URL url) {
// TODO Auto-generated method stub
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Uri uri = Uri.parse((String) links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
So can someone help me out in implementing a simple progressbar when loading the data? :)
Upvotes: 0
Views: 3530
Reputation: 11439
You should use an AsyncTask to perform all of you downloading stuff. Then in the AsyncTask's onProgessUpdate method, refresh the adapter to the listview.
class LoadingTask extends AsyncTask<URL, Integer, Integer> {
ProgressDialog mDialog;
BaseAdapter mAdapter
public LoadingTask(Context context, BaseAdapter adapter) {
mDialog = new ProgressDialog(context);
mAdapter = adapter;
// Do your dialog stuff here
}
@Override
public Integer doInBackground(URL... urls) {
int result = 0; // number of loaded entities
while (needsToDownload) {
// Do your downloading here
updateProgress(result / urls.length);
}
return result;
}
@Override
public void onProgressUpdate(Integer... ints) {
mDialog.setProgress(ints[0]);
mAdapter.notifyDatasetChanged()
}
public void onPostExecute(Integer result) {
mDialog.dismiss()
Toast.makeToast(context, "Loaded " + result + " urls...", 1).show();
}
}
Upvotes: 1