gpt4 book ai didi

java - Android使用sharedpreferences更改数据后更新listview

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:56 26 4
gpt4 key购买 nike

我正在尝试实现 PreferenceFragmentCompatSharedPreferences.OnSharedPreferenceChangeListener

我的应用程序由主要 Activity 和 fragment 组成。主页 fragment 有一个带有标题的 URL 列表,我想添加一个设置以将 URL 添加到此列表。这是我到目前为止所尝试过的:

这是SettingsFragment.java:

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference preference = findPreference(key);
if (preference instanceof EditTextPreference) {
EditTextPreference editTextPreference = (EditTextPreference) preference;
String value = editTextPreference.getText();
new HomeFragment().addLink(value);
} else {
assert preference != null;
preference.setSummary(sharedPreferences.getString(key, ""));

}
}

还有HomeFragment.java:

private ArrayList<LinkItem> urls = new ArrayList<>(Arrays.asList(
new LinkItem("LifeHacker RSS Feed", "https://lifehacker.com/rss"),
new LinkItem("Google News Feed", "https://news.google.com/news/rss");
private LinkItemAdapter itemAdapter;
private ListView listView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
listView = view.findViewById(R.id.postListView);
itemAdapter = new LinkItemAdapter(getActivity(), R.layout.link_item, urls);
listView.setAdapter(itemAdapter);
listView.setOnItemClickListener(onItemClickListener);
itemAdapter.notifyDataSetChanged();
return view;
}

void addLink(String title) {
urls.add(new LinkItem(title, "https://google.com"));
itemAdapter.notifyDataSetChanged();
}

private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HomeFragmentDirections.ActionHomeFragmentToRssFragment action =
HomeFragmentDirections.actionHomeFragmentToRssFragment(urls.get(position).Link, urls.get(position).Title);
NavHostFragment.findNavController(HomeFragment.this).navigate(action);
}
};

如果我尝试这样做,itemAdapter 将为空,导致应用程序崩溃,所以我不确定如何实现它。如果我尝试像 HomeFragment 的 onCreate 方法一样在 addLink 中重新创建它,则该 Activity 最终将为空。如果我尝试从设置 fragment 传递 Activity 或上下文,则会发生相同的结果。

LinkItemAdapter 适应以下对象:

public class LinkItem {
public String Title;
public String Link;
}

到目前为止,我的结果始终是相同的:由于空指针,在更改编辑文本字段后单击“确定”时就会崩溃。有人可以帮我解决这个问题吗?我是安卓新手。

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfragmentapp, PID: 5185
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.myfragmentapp.adapters.LinkItemAdapter.notifyDataSetChanged()' on a null object reference
at com.example.myfragmentapp.screens.HomeFragment.addLink(HomeFragment.java:86)
at com.example.myfragmentapp.screens.SettingsFragment.onSharedPreferenceChanged(SettingsFragment.java:42)
at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:560)
at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:443)
at androidx.preference.Preference.tryCommit(Preference.java:1632)
at androidx.preference.Preference.persistString(Preference.java:1663)
at androidx.preference.EditTextPreference.setText(EditTextPreference.java:80)
at androidx.preference.EditTextPreferenceDialogFragmentCompat.onDialogClosed(EditTextPreferenceDialogFragmentCompat.java:99)
at androidx.preference.PreferenceDialogFragmentCompat.onDismiss(PreferenceDialogFragmentCompat.java:267)
at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1377)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6709)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

最佳答案

您应该在创建适配器之后调用addLink():

 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
listView = view.findViewById(R.id.postListView);
itemAdapter = new LinkItemAdapter(getActivity(), R.layout.link_item, urls);
listView.setAdapter(itemAdapter);
listView.setOnItemClickListener(onItemClickListener);
addLnk();
return view;
}

如果您尝试将一个值从一个 fragment 设置为另一个 fragment ,您应该使用回调或 ViewModel,其中更简单的是回调:

定义回调接口(interface):

interface OnSetPreferenceItem{
void setPrefItemInList(String item);
}

在SettingsFragment中,定义一个变量:

 private OnSetPreferenceItem callback;

在同一个fragment中,在onAttach中填写变量:

 public void onAttach(Context context) {
super.onAttach(context);

callback = (OnSetPreferenceItem )context;
}

现在不再调用 new HomeFragment().addLink(value);,而是调用

callback.setPrefItemInList(value);

让您的父 Activity 实现该接口(interface)并实现建议的方法:

public void setPrefItemInList(String item){
homeFragment.addLink(item);
}

修改您的 addLink 方法以保护它:

void addLink(String title) {
urls.add(new LinkItem(title, "https://google.com"));
if(itemAdapter!=null){
itemAdapter.notifyDataSetChanged();
}
}

关于java - Android使用sharedpreferences更改数据后更新listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com