notifyDataSetChanged() 方法不起作用
notifyDataSetChanged() method not working
我正在使用 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;
}
}
}
我刷新的时间不对吗?
您的 mAdapter
使用 getData()
,每次 new ArrayList<>()
的另一个实例以及当您调用 mAdapter.notifyDataSetChanged();
时,适配器都会尝试刷新旧的 returns arrayList 的实例,但没有任何变化。
您需要将数据保存到一个字段中并像 mArrayList.clear()
和 mArrayList.addAll(getData())
那样更新该字段。
我正在使用 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;
}
}
}
我刷新的时间不对吗?
您的 mAdapter
使用 getData()
,每次 new ArrayList<>()
的另一个实例以及当您调用 mAdapter.notifyDataSetChanged();
时,适配器都会尝试刷新旧的 returns arrayList 的实例,但没有任何变化。
您需要将数据保存到一个字段中并像 mArrayList.clear()
和 mArrayList.addAll(getData())
那样更新该字段。