I'm trying to implement a search function..For that i used a searchview and implement a onQueryTextChange method but currently it filter data (Confirm with Log and display A Toast Message Toast.makeText(getApplicationContext(),temp.size()+toString(),Toast.LENGTH_SHORT).show();) (Here it will identify the recoreds based on user Input and display a Toast) but cannot update a recyclerview based on user Input and display a updated or filtered result..
editText1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<addAccount> dataholder = new ArrayList<>();
final Dialog dialog = new Dialog(BillEntry.this);
dialog.setContentView(R.layout.searchable_list_dialog);
dialog.setTitle("Title");
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = (int) (displayMetrics.heightPixels * 0.4);
int width = (int) (displayMetrics.widthPixels * 0.9);
dialog.getWindow().setLayout(width, height);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
dialog.show();
recyclerView = dialog.findViewById(R.id.listItems);
LinearLayoutManager layoutManager = new LinearLayoutManager(dialog.getContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new billEntryDialog(accounts);
recyclerView.setAdapter(adapter);
billEntryDialog adapter = new billEntryDialog(dataholder);
displayAccountsFromJson(jsonData);
SearchView search = dialog.findViewById(R.id.search);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
// filter(newText.toString());
return true;
}
});
Button btnclosedialog = dialog.findViewById(R.id.btnClose);
btnclosedialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Closed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
});
void filter(String s){
ArrayList<addAccount> temp = new ArrayList<>();
for ( addAccount item:accounts){
if (item.getEdtAccountName().contains(s)){
Log.d("Filter", "Filtered item: " + item.getEdtAccountName());
temp.add(item);
}
}
Log.d("SIzeddddd", "Filtered item: " + temp.size());(Log Filter item based on input)
adapter.updatelist(temp);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
billEntryDialog.java (Adapter)
public class billEntryDialog extends RecyclerView.Adapter<billEntryDialog.ViewHolder> implements Filterable {
private ArrayList<addAccount> dataList;
private ArrayList<addAccount> dataListFiltered;
private FilterListener filterListener;
public interface FilterListener {
void onFilter(String query);
}
public billEntryDialog(ArrayList<addAccount> data) {
this.dataList = data;
this.dataListFiltered = data;
}
public void updatelist(ArrayList<addAccount> temp){
this.dataList = temp;
notifyDataSetChanged();
}
public void setFilterListener(FilterListener listener) {
this.filterListener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_entry_account, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
addAccount data = dataListFiltered.get(position);
holder.textView.setText(data.getEdtAccountName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (filterListener != null) {
filterListener.onFilter(data.getEdtAccountName());
}
}
});
}
@Override
public int getItemCount() {
return dataListFiltered.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String query = constraint.toString().toLowerCase();
ArrayList<addAccount> tempFilteredList = new ArrayList<>();
if (query.isEmpty()) {
tempFilteredList.addAll(dataList);
} else {
for (addAccount item : dataList) {
if (item.getEdtAccountName().toLowerCase().contains(query)) {
tempFilteredList.add(item);
}
}
}
dataList=tempFilteredList;
notifyDataSetChanged();
FilterResults filterResults = new FilterResults();
filterResults.values = tempFilteredList;
Log.d("Storeddddddddd", tempFilteredList.toString());
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
dataListFiltered.clear();
dataListFiltered.addAll((List<addAccount>) results.values);
notifyDataSetChanged();
}
};
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.txtName); // Replace with actual TextView ID
}
}
}
addAccount.java (Model) (Don't know if it's require here)
public class addAccount {
private String edtAccountName,edtOpeningAmt,edtOpeningFine;
public addAccount(String edtAccountName, String edtOpeningAmt, String edtOpeningFine) {
this.edtAccountName = edtAccountName;
this.edtOpeningAmt = edtOpeningAmt;
this.edtOpeningFine = edtOpeningFine;
}
public addAccount(String edtAccountName) {
this.edtAccountName = edtAccountName;
}
public String getEdtAccountName() {
return edtAccountName;
}
public void setEdtAccountName(String edtAccountName) {
this.edtAccountName = edtAccountName;
}
public String getEdtOpeningAmt() {
return edtOpeningAmt;
}
public void setEdtOpeningAmt(String edtOpeningAmt) {
this.edtOpeningAmt = edtOpeningAmt;
}
public String getEdtOpeningFine() {
return edtOpeningFine;
}
public void setEdtOpeningFine(String edtOpeningFine) {
this.edtOpeningFine = edtOpeningFine;
}
}