gpt4 book ai didi

java - 我的 RecyclerView 在过滤后不断重置

转载 作者:行者123 更新时间:2023-12-02 01:14:25 27 4
gpt4 key购买 nike

我有一个可用的 RecyclerView,我需要实现一个搜索栏。我设法用 EditText 和 TextWatcher 创建一个搜索栏,但遇到以下问题:

通常,我的列表根据当前项目的位置传递数据,当我过滤列表时,位置当然会搞砸。

我遵循了一些指南,发现了这个解决方法:

private void filter(String text){
ArrayList<Item> filteredList = new ArrayList<>();

for (Item item : mList ) {
if(item.getName().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mListAdapter.filterList(filteredList);

//when the app is done filtering, it clears the existing ArrayList and it adds just the filtered Items

mList.clear();
mList.addAll(filteredList);
}

这种方法的问题在于,当 mList.clear() 发生时,我会丢失所有数据,只保留过滤后的数据。然后,当我从搜索栏中删除文本时,我所拥有的只是一个空的 RecyclerView。

我考虑过当 EditText 为空时尝试将列表重置为原始状态,但我不知道该怎么做,而且这并不理想。

有人能想出办法解决这个问题吗?我真的很绝望:D

我的 RecyclerView 类:

public class ListFragment extends Fragment {

static final String EXTRA_NAME = "monumentname";
static final String EXTRA_NUMBER = "monumentnumber";
static final String EXTRA_REGION = "monumentregion";
static final String EXTRA_REGION2 = "monumentregion2";
static final String EXTRA_TOWN = "monumenttown";
static final String EXTRA_DESCRIPTION = "monumentdescription";
static final String EXTRA_WEB = "web";

private ListAdapter mListAdapter;
private ArrayList<Item> mList;
private RequestQueue mRequestQueue;
private Context mContext;
private InternetCheck internetCheck = new InternetCheck();


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_list, container, false);

RecyclerView mRecyclerView = rootview.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Button syncData = rootview.findViewById(R.id.sync_button);
final ProgressBar progressBar = rootview.findViewById(R.id.progressbar);
EditText editText = rootview.findViewById(R.id.edittext);

editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});

..........some other irrelevant stuff here


mListAdapter.setOnItemClickListener(new ListAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Intent detailIntent = new Intent(getActivity(), SingleItem.class);
Item clickedItem = mList.get(position);

detailIntent.putExtra(EXTRA_NAME, clickedItem.getName());
detailIntent.putExtra(EXTRA_NUMBER, clickedItem.getNumber());
detailIntent.putExtra(EXTRA_REGION, clickedItem.getRegion());
detailIntent.putExtra(EXTRA_REGION2, clickedItem.getRegion2());
detailIntent.putExtra(EXTRA_TOWN, clickedItem.getTown());
detailIntent.putExtra(EXTRA_DESCRIPTION, clickedItem.getDescription());
detailIntent.putExtra(EXTRA_WEB, clickedItem.getWeb());

startActivity(detailIntent);
}
});


return rootview;
}

.......some onCreateView and stuff like that here

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

loadData();
mListAdapter = new ListAdapter(mContext, mList);
mRequestQueue = Volley.newRequestQueue(mContext);

}

private void parseJSON() {

JsonObjectRequest request = new JsonObjectRequest("http://192.168.0.105/sestavsisvujsvetweb/api/seznammagnetek", null, new Response.Listener<JSONObject>() {

.....parsing my JSON here



mRequestQueue.add(request);

}

private CharSequence removeHtmlFrom(String html) {
return new HtmlCleaner().clean(html).getText();
}

@Override
public void onStop() {
super.onStop();
saveData();
}


private void saveData() {
SharedPreferences sharedPreferences = getContext().getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(mList);
editor.putString("seznam magnetek", json);
editor.apply();
}

private void loadData() {
SharedPreferences sharedPreferences = getContext().getSharedPreferences("shared preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("seznam magnetek", null);
Type type = new TypeToken<ArrayList<Item>>() {
}.getType();
mList = gson.fromJson(json, type);

if (mList == null || mList.isEmpty()) {
Toast.makeText(getContext(), "Seznam magnetek je prázdný. Aktualizujte prosím data z databáze.", Toast.LENGTH_SHORT).show();
mList = new ArrayList<>();
}
}

private void filter(String text) {
ArrayList<Item> filteredList = new ArrayList<>();

for (Item item : mList) {
if (item.getName().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mListAdapter.filterList(filteredList);
mList.clear();
mList.addAll(filteredList);
}

}

最佳答案

这里有 2 个潜在问题:

  1. 将filteredList传递给mListAdapter后,您必须调用adapter.notifyDataSetChanged notifyDataSetChanged使 ListView 无效。
  2. 此外,将过滤后的列表添加到适配器后,您不需要清除mList。由于 loadData 仅调用一次,因此如果您第一次清除此设置,列表中将仅保留过滤后的项目。即使您删除过滤字符串,该项目也不会添加回mList

请尝试添加以下内容:

private void filter(String text){
ArrayList<Item> filteredList = new ArrayList<>();

for (Item item : mList ) {
if(item.getName().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mListAdapter.filterList(filteredList);
mListAdapter.notifyDataSetChanged();
}

关于java - 我的 RecyclerView 在过滤后不断重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57666232/

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