2017-03-22 14 views
0

私のリストビューデータは、アクティビティをリロードしたり、関連するアクティビティに戻るときに重複します。しかし、ログアウト後にアプリケーションからログインすると、一意のデータが保持されます。リストビューで一意のデータを表示する方法

私のコードをデバッグすると、データベースのカーソルが以前の値を保持していることが分かります。これを解決するには?

public void setadapter() { 
    boolean checkFirstTime = false; 

    Log.e("Set Adapter call>>>>>", "called"); 
    HashMap<String, String> map; 

    Database1 db = new Database1(getActivity()); 
    Cursor cursor = db.getWritableDatabase().query("timelinedata", null, null, null, null, null, null); 
    System.out.println("Size of Cursor" + cursor.getCount()); 
    TImelineTask.contactList.clear(); 
    TImelineTask.contactList = new ArrayList<>(); 

    while (cursor.moveToNext()) { 

     map = new HashMap<>(); 

     map.put("id", cursor.getString(0)); 
     map.put("sms", cursor.getString(1)); 
     map.put("phoneno", cursor.getString(2)); 
     map.put("date_time", cursor.getString(3)); 
     map.put("type", cursor.getString(4)); 
     map.put("source", cursor.getString(5) + " "); 
     map.put("imagepath", cursor.getString(6)); 
     map.put("imageadd", cursor.getString(7) + ""); 
     map.put("issue_type", cursor.getString(8) + ""); 
     map.put("issue_select_type", cursor.getString(9)); 
     map.put("issuetype", cursor.getString(9)); 
     map.put("date", cursor.getString(10)); 
     map.put("time", cursor.getString(11)); 

     if (getActivity().getIntent().getStringExtra("caseid").equalsIgnoreCase(cursor.getString(13))) { 

      TImelineTask.contactList.add(map); 
     } 


    } 
    if (cursor != null && !cursor.isClosed()) { 
     cursor.close(); 
    } 
    db.close(); 
    if (checkFirstTime) { 
     if (TImelineTask.contactList.size() > 0) { 
      TImelineTask.contactList.clear(); 
     } 
    } 


    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    for (int i = 0; i < TImelineTask.contactList.size() - 1; i++) { 
     try { 
      Date date1 = sdf.parse(TImelineTask.contactList.get(i).get("date_time")); 
      swappos = i; 
      for (int j = i + 1; j < TImelineTask.contactList.size(); j++) { 
       Date date2 = sdf.parse(TImelineTask.contactList.get(j).get("date_time")); 

       if (date1.compareTo(date2) < 0) { 

        Collections.swap(TImelineTask.contactList, swappos, j); 
        date1 = date2; 

       } 
      } 
     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

    } 
    Log.e("Adapter ", "calling"); 
    System.out.println("Adapter Size" + TImelineTask.contactList.size()); 
    listadapter = new ListAdapterClass(TImelineTask.contactList, getActivity(), this); 


    listview.setAdapter(listadapter); 
    listadapter.notifyDataSetChanged(); 
    if (listadapter.isEmpty()) { 
     noentry.setVisibility(View.VISIBLE); 
     listadapter.notifyDataSetChanged(); 
    } else { 
     listadapter.notifyDataSetChanged(); 
     noentry.setVisibility(View.GONE); 
    } 

    new MyTimerTask().execute(); 
    checkFirstTime = true; 
} 
+1

リストにデータを追加する前にリストを消去します。 – Piyush

+0

@Piyush私はそれを試しましたが、何も起こっていません。 – MonicaGPH

+0

データベースに重複したデータがありますか? – fangxing

答えて

0

あなたはOnPause()またはのOnStart()あなたのリストをクリアするためのアクティビティのメソッドをオーバーライドすることができます

はここに私のコードです。

活動

0

のライフサイクルについての記事を読む私はそれがデータを複製して..Actuallyデータベースカーソルは前の値を取得している私のミスをキャッチ。

関連する問題