I am trying to remove duplicate values from an ArrayList
using HashSet
, so that city names aren’t returned more than once if they are the sameā¦ The List comes back either empty or still shows duplicate values. Was hoping someone could tell me where the error is in my code, so that no duplicate values are returned…
I am using this for reference:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
, but can’t figure out how to make it work in my code. Know it’s probably a simple fix, but I’ve been playing around with it and still hasn’t come back right…
Anyone can tell me where I am going wrong here? According to everyone so far the code looks like it should be working… Although it’s not…
SearchCityFragment
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; } } }
Advertisement
Answer
if you only want to show the post with searched city and you want to show only one such post you can just query the database for post with that city , you don’t need to create any arraylist or hashmap.
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) {} } ; }