2016-10-12 128 views
0

我有一個多個EditText的活動,輸入數據後點擊按鈕SAVE將它們保存在數據庫(Mysql)中,它打開Fragment,其中有來自數據庫的由此數據填充的ListView。 問題:ListView沒有立即更新片段

ListView沒有顯示我在活動!!!中輸入的新數據,即使新數據正確添加到我的ArrayList中。 但是,當我第二次啓動這個片段時,它會正確顯示帶有新數據的Listview。

我的片段

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_liste_symbole_monetaire, container, false); 
    symbolMonList.clear(); 
    lv = (SwipeMenuListView) rootView.findViewById(R.id.lv_liste_symboleMon); 
    adapter = new SymbMoneLvAdapter(getActivity()); 

    FloatingActionButton btn = (FloatingActionButton) rootView.findViewById(R.id.btnAjoutAjoutSymboleMon); 
    btn.setOnClickListener(new android.view.View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Intent intent = new Intent(getActivity(), AjouterSymbMoneActivity.class); 
      startActivity(intent); 

     } 

    }); 
    deleteItemLv(); 
    getListSymbolMon(); 

    return rootView; 
} 

public void deleteItemLv(){ 
    SwipeMenuCreator creator = new SwipeMenuCreator() { 

     @Override 
     public void create(SwipeMenu menu) { 
      // create "open" item 
      SwipeMenuItem openItem = new SwipeMenuItem(
        getActivity()); 
      // set item background 
      openItem.setBackground(new ColorDrawable(Color.GRAY)); 
      // set item width 
      openItem.setWidth(dp2px(90)); 
      // set item title 
      openItem.setTitle("Ouvrir"); 
      // set item title fontsize 
      openItem.setTitleSize(18); 
      // set item title font color 
      openItem.setTitleColor(Color.WHITE); 
      // add to menu 
      menu.addMenuItem(openItem); 

      // create "delete" item 
      SwipeMenuItem deleteItem = new SwipeMenuItem(
        getActivity()); 
      // set item background 
      deleteItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 
        0xCE))); 
      // set item width 
      deleteItem.setWidth(dp2px(90)); 
      // set a icon 
      deleteItem.setIcon(R.drawable.ic_delete); 
      // add to menu 
      menu.addMenuItem(deleteItem); 
     } 
    }; 

    lv.setMenuCreator(creator); 
    lv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { 
     @Override 
     public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { 
      switch (index) { 
       case 0: 
        //Ouvrir item 
        break; 
       case 1: 
        deleteItemDialog(position); 
        break; 

      } 
      // false : close the menu; true : not close the menu 
      return false; 
     } 
    }); 

} 


public int dp2px(int dp) { 
    DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics(); 
    int px = Math.round(dp * (displayMetrics.xdpi/DisplayMetrics.DENSITY_DEFAULT)); 
    return px; 
} 

//Dialog delete item: 
public void deleteItemDialog(final int position){ 
    Toast toast = Toast.makeText(getActivity(), "size:"+position, Toast.LENGTH_LONG); 
    toast.show(); 
    Toast toat = Toast.makeText(getActivity(), "sizeTotal:"+symbolMonList.size(), Toast.LENGTH_LONG); 
    toat.show(); 

    Toast oast = Toast.makeText(getActivity(), "code:"+symbolMonList.get(position).getCode(), Toast.LENGTH_LONG); 
    oast.show(); 


    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      getActivity()); 
    alertDialogBuilder.setTitle("Supprimer"); 
    alertDialogBuilder.setMessage("Voulez vous supprimer ce symbole monétaire?"); 
    alertDialogBuilder 
      .setCancelable(false) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          String Code = symbolMonList.get(position).getCode(); 
          Call<Void> api =API.deleteSymboleMon("delete", Code); 
          api.enqueue(new Callback<Void>() { 
           @Override 
           public void onResponse(Call<Void> call, Response<Void> response) { 
            if(response.isSuccessful()){ 
             Toast toast = Toast.makeText(getActivity(), "Symbole Monétaire supprimé", Toast.LENGTH_LONG); 
             toast.show(); 
             symbolMonList.remove(position); 
             adapter.notifyDataSetChanged(); 

            }else { 
            } 

           } 

           @Override 
           public void onFailure(Call<Void> call, Throwable t) { 

           } 
          }); 


         } 
        } 

      ) 
      .setNegativeButton("Annuler", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        } 

      ); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 

} 

//Get liste des SM 
private void getListSymbolMon(){ 
    final ProgressDialog mProgressDialog = new ProgressDialog(getActivity()); 
    mProgressDialog.setIndeterminate(true); 
    mProgressDialog.setMessage("Chargement en cours..."); 

    Call<List<SymbMoneItems>> api =API.getListSymbolMon(); 

    api.enqueue(new Callback<List<SymbMoneItems>>() { 
     @Override 
     public void onResponse(Call<List<SymbMoneItems>> call, Response<List<SymbMoneItems>> response) { 
      if (response.isSuccessful()){ 
       List<SymbMoneItems> List = response.body(); 
       addListSymbolMon(List); 
       if (mProgressDialog.isShowing()) 
        mProgressDialog.dismiss(); 

      }else{ 
       if (mProgressDialog.isShowing()) 
        mProgressDialog.dismiss(); 

       Toast toast = Toast.makeText(getActivity(), "Erreur", Toast.LENGTH_LONG); 
       toast.show(); 

      } 
     } 

     @Override 
     public void onFailure(Call<List<SymbMoneItems>> call, Throwable t) { 
      Toast toast = Toast.makeText(getActivity(), "Erreur "+t, Toast.LENGTH_LONG); 
      toast.show(); 
      if (mProgressDialog.isShowing()) 
       mProgressDialog.dismiss(); 


     } 
    }); 
} 
public void addListSymbolMon(List<SymbMoneItems> lem){ 
    for (int i=0;i< lem.size();i ++){ 
     SymbMoneItems fac = lem.get(i); 
     symbolMonList.add(fac); 

    } 
    adapter.addAll(symbolMonList); 
    adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 

} 

在我的適配器

public void addAll(ArrayList<SymbMoneItems> result) { 
    this.listSymb = result; 
    this.notifyDataSetChanged(); 
} 

感謝您的幫助。

+0

你是如何從數據庫中獲取數據?你有沒有使用CursorAdapter? (我的猜測是否定,但只是驗證) – AdamMc331

+0

不,我使用BaseAdapter –

回答

0

使用「=」設置數據實際上對適配器的數據集沒有任何更改。

更改您的addAll()方法,象下面這樣:

public void addAll(ArrayList<SymbMoneItems> result) { 
    this.listSymb.clear(); 
    this.listSymb.addAll(result); 
    this.notifyDataSetChanged(); 
} 
+0

我試過了,但仍然無法正常工作 –