使用 HashSet 清除重复值.List 返回空值或返回重复值



我正在尝试使用HashSetArrayList中删除重复值,以便如果城市名称相同,则不会多次返回......列表返回时为空或仍显示重复值。希望有人能告诉我我的代码中的错误在哪里,这样就不会返回重复的值......

我用这个作为参考:

Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);,但无法弄清楚如何使其在我的代码中工作。知道这可能是一个简单的修复,但我一直在玩它,但仍然没有回来......

谁能告诉我我哪里出了问题?根据到目前为止的每个人的说法,代码看起来应该可以工作......虽然不是...

搜索城市片段

public class SearchCityFragment extends Fragment {
private List<Post> mPostList;
private Set<Post> mPostSet;
private RecyclerView mRecyclerView;
private CityAdapter mCityAdapter;
private EditText mSearchBar;
private RelativeLayout mRelativeLayout;
private Activity mActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_city, container, false);
mRelativeLayout = view.findViewById(R.id.relative_layout_11);
mRelativeLayout.setVisibility(View.VISIBLE);
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(linearLayoutManager);
mPostList = new ArrayList<>();
mCityAdapter = new CityAdapter(getContext(), mPostList);
mRecyclerView.setAdapter(mCityAdapter);
mSearchBar = mActivity.findViewById(R.id.search_bar);
mSearchBar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchCity(s.toString().toLowerCase());
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
private void searchCity(String s) {
Query query = FirebaseDatabase.getInstance().getReference("Posts").orderByChild("city").startAt(s).endAt(s + "uf8ff");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mPostList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (s.length() == 0) {
mPostList.clear();
mRelativeLayout.setVisibility(View.VISIBLE);
} else {
mRelativeLayout.setVisibility(View.GONE);
mPostList.add(post);
mPostSet = new HashSet<>(mPostList);
mPostList.clear();
mPostList.addAll(mPostSet);
readCity();
}
}
mCityAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readCity() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (mSearchBar.getText().toString().equals("")) {
mPostList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
mPostList.add(post);
mPostSet = new HashSet<>(mPostList);
mPostList.clear();
mPostList.addAll(mPostSet);
}
mCityAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof Activity) {
mActivity = (Activity) context;
}
}
}

如果您只想显示搜索城市的帖子,并且只想显示一个这样的帖子,则可以在数据库中查询该城市的帖子,您无需创建任何 arraylist 或哈希图。

private void searchCity(String s) {
FirebaseDatabase.getInstance()
.getReference("Posts")
.orderByChild("city")
.startAt(s)
.limitToFirst(1).
.addValueEventListener(new ValueEvnetListener){
@Override
public void onDataChange(DataSnapshot 
dataSnapshot) {
// do your rhing
}
@Override
public void onCancelled(DatabaseError databaseError) {}
} ;
}

相关内容

  • 没有找到相关文章

最新更新