SwipeRefreshLayout is very useful when we work with data from Internet or Server. With a simple action, we'll update data.
In this post, I present a simple project that:
- Get JSON data from an URL.
- Parse JSON and populating data to a ListView.
- Swipe to refresh/ update data.
1. Lauch Eclipse and start a new Android Project (min-sdk is 14).
2. Create a model object for our project (Student.java):
3. Create layout for launching activity (activity_swipe.xml):
4. Give a Custom AsyncTask to get data from Internet through an URL like this:
5. In SwipeActivity, we must:
- set OnRefreshListener for SwipeRefreshLayout.
- execute AsyncTask and get data after it's done.
- set/notify adapter for ListView.
Full code:
6. Okey, these are some necessary class/ layout in project:
- A custom Adapter for ListView:
- Layout for each ListView row(item_listview.xml):
- Some own colors use in projects (res/values/colors.xml):
7. Some result screens:
(Sorry for having ads)