2015-10-23 148 views
0

這是什麼可能的解決方案。 我有一個刷卡下載刷新listview在我的應用程序。但是當我嘗試刷新它時,它會創建重複的行。 我希望以前更新的行保留在視圖中,當我刷卡更新它應追加更新行(這是工作正常)頂部的視圖。 唯一的問題是行重複。 這是我的代碼。刷新列表視圖重複行每次我刷新我的列表視圖

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) 

回答

3

只需撥打

if(movieList!=null) { 
    movieList.clear(); 
} 

獲取電影之前

+0

笑納的答案,如果它幫助 – vaibhav

+0

你好Vaibhav的,這是工作,但應用程序崩潰,如果我刷新3次 – Shelby

+0

我已經把代碼爲第聲明fetchMovies()方法 – Shelby

2

遵循以下步驟:

  1. 在調用onRefresh()時清除列表視圖中的所有項目。
  2. 打電話給webservice並獲取電影列表。
  3. 設置列表適配器。