Reputation: 81
What is the possible solution to this.
i have a swipe down to refresh listview
in my app. but when i try to refresh it,it creates duplicate rows.
i want the previously updated rows to remain in the view and when i swipe to update it should append the view on top with updated rows(which is workin fine).
only problem is duplication of rows.
here is my code.
public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
private String TAG = MainActivity.class.getSimpleName();
private String URL_TOP_250 = "My_url";
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private SwipeListAdapter adapter;
private List<Movie> movieList;
// initially offset will be 0, later will be updated while parsing the json
private int offSet = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
movieList = new ArrayList<>();
adapter = new SwipeListAdapter(this, movieList);
listView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(this);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
fetchMovies();
}
}
);
}
/**
* This method is called when swipe refresh is pulled down
*/
@Override
public void onRefresh() {
fetchMovies();
}
/**
* Fetching movies json by making http call
*/
private void fetchMovies() {
// showing refresh animation before making http call
swipeRefreshLayout.setRefreshing(true);
String url = URL_TOP_250;
// Volley's json array request object
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
if (response.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < response.length(); i++) {
try {
JSONObject movieObj = response.getJSONObject(i);
String title = movieObj.getString("title");
String desc = movieObj.getString("desc");
Movie m = new Movie(title,desc);
movieList.add(0,m);
// HashSet hs = new HashSet();
// hs.addAll(al);
//al.clear();
//al.addAll(hs);
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(req);
}
}
**LOG****************
10-23 16:28:14.959 17691-17691/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x400207d8)
10-23 16:28:14.969 17691-17691/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at info.androidhive.swiperefresh.helper.SwipeListAdapter.getView(SwipeListAdapter.java:60)
at android.widget.AbsListView.obtainView(AbsListView.java:1427)
at android.widget.ListView.makeAndAddView(ListView.java:1802)
at android.widget.ListView.fillDown(ListView.java:727)
at android.widget.ListView.fillGap(ListView.java:698)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3390)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:2267)
at android.widget.ListView.onTouchEvent(ListView.java:3617)
at android.view.View.dispatchTouchEvent(View.java:3766)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1731)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1120)
at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1715)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1787)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 2421
Reputation: 1736
Follow these steps:
Upvotes: 2
Reputation: 816
Just call
if(movieList!=null) {
movieList.clear();
}
before fetching movies
Upvotes: 4