notifyDataSetChanged()方法不起作用

时间:2015-10-09 12:04:32

标签: android refresh android-recyclerview dataadapter

我正在使用RecyclerView,在用户输入后,我想更新RecyclerView。但根本没有发生任何事情。 当我再次重新加载整个Fragment时,将刷新布局。

我的代码:

    package com.stack.overflow.fragment.Einstellungen;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import com.stack.overflow.HelperClasses.RecyclerView.DividerItemDecoration;
    import com.stack.overflow.HelperClasses.RecyclerView.ItemClickSupport;
    import com.stack.overflow.R;
    import com.stack.overflow.fragment.Einstellungen.QrCode.FragmentQrCode;
    import com.afollestad.materialdialogs.MaterialDialog;
    import com.pixplicity.easyprefs.library.Prefs;

    import java.util.ArrayList;
    import java.util.List;

    public class FragmentEinstellungen extends Fragment {
        private static final String KEY_STUFE = "Stufe";
        private static final String DEFAULT_EINTRAG_STUFE = "keine Stufe ausgewählt";

        private RecyclerView mRecyclerView;
        private EinstellungenDataAdapter mAdapter;
        private static String[] titles = null;
        private static String[] titlesStufen = null;
        private static String[] titlesKlassen = null;

        int callbackDialog;
        String stufe = "";

        public FragmentEinstellungen(){}

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            titles = getActivity().getResources().getStringArray(R.array.einstellungen);
            titlesStufen = getActivity().getResources().getStringArray(R.array.stufenauswahl);
            titlesKlassen = getActivity().getResources().getStringArray(R.array.klassenauswahl);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_einstellungen, container, false);
            mRecyclerView = (RecyclerView) layout.findViewById(R.id.einstellungenRecyclerView);

            mAdapter = new EinstellungenDataAdapter(getActivity(), getData());
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

            final ItemClickSupport itemClick = ItemClickSupport.addTo(mRecyclerView);

            itemClick.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View child, int position, long id) {

                     //SnackbarManager.show(
                     //       Snackbar.with(getActivity())
                     //               .text("KLICK"));

                     displayView(0);
                }
            });

            return layout;
        }

        public static List<EinstellungenItem> getData() {
            List<EinstellungenItem> data = new ArrayList<>();

            // preparing navigation drawer items
            for (int i = 0; i < titles.length; i++) {
                EinstellungenItem navItem = new EinstellungenItem();
                navItem.setTitle(titles[i]);

                if(i==0){
                    navItem.setSubtitle("kein gültiger Scan");
                } else if(i==1){
                    navItem.setSubtitle(Prefs.getString(KEY_STUFE, DEFAULT_EINTRAG_STUFE));
                }

                data.add(navItem);
            }
            return data;
        }

 private void displayView(int position) {

        switch (position) {
            case 0:
            break;
            case 1:

                new MaterialDialog.Builder(getActivity())
                        .title(R.string.title_dialog_stufe)
                        .items(R.array.stufenauswahl)
                        .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
                            @Override
                            public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                callbackDialog = which;

                                stufe = titlesStufen[which];


                                if (callbackDialog <= 4) {
                                    new MaterialDialog.Builder(getActivity())
                                            .title(R.string.title_dialog_klasse)
                                            .items(R.array.klassenauswahl)
                                            .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
                                                @Override
                                                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {

                                                    stufe = stufe + titlesKlassen[which];

                                                    Prefs.putString(KEY_STUFE, stufe);
                                                    mAdapter.notifyDataSetChanged();

                                                    return true;
                                                }
                                            })
                                            .positiveText(R.string.choosetext_dialog_klasse)
                                            .show();


                                } else {

                                    Prefs.putString(KEY_STUFE, stufe);
                                    mAdapter.notifyDataSetChanged();
                                }

                                return true;
                            }
                        })
                        .positiveText(R.string.choosetext_dialog_stufe)
                        .show();

                callbackDialog = 0;

            break;
            default:
                break;
        }


    }

}

我在错误的时间刷新了吗?

1 个答案:

答案 0 :(得分:1)

您的mAdapter使用getData(),每当new ArrayList<>()的另一个实例返回时,当您调用mAdapter.notifyDataSetChanged();时,适配器会尝试刷新arrayList的旧实例但是那里没有变化。

您需要将数据保存到字段中并更新该字段,例如mArrayList.clear()mArrayList.addAll(getData())