ListView未在Fragment

时间:2016-10-12 17:08:41

标签: android listview fragment

我有一个带有多个EditText的活动,在输入数据后单击按钮SAVE将它们保存在数据库(Mysql)中它打开片段,其中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();
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用“=”设置数据实际上对适配器的数据集没有任何改变。

更改你的addAll()方法,如下所示:

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