I have tried the below code in my fragment class but it is not working and also not showing any error,i have given both the adapter class code and fragment class code please tell where i have mistaken what is happening that when i run the code it is not showing even the search bar on the fragment
//Fragment class code
public class PharmacyPatientFragment extends Fragment {
PharmacyAdapter Adapter;
private FragmentPharmacyPatientBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentPharmacyPatientBinding.inflate(inflater);
binding.recylerviewPharmacy.setLayoutManager(new LinearLayoutManager(getContext()));
Adapter = new PharmacyAdapter(dataqueue());
binding.recylerviewPharmacy.setAdapter(Adapter);
return binding.getRoot();
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.search_option, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
super.onCreateOptionsMenu(menu, inflater);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
Adapter.getFilter().filter(s);
return false;
}
});
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public ArrayList<PharmacyDataModel> dataqueue() {
ArrayList<PharmacyDataModel> holder = new ArrayList<>();
PharmacyDataModel obj1 = new PharmacyDataModel();
obj1.setMed_name("Remdesiver");
obj1.setMfg_change("HealthBio Pharmaceuticals");
obj1.setPrice_change("10000Rs.");
obj1.setQty_change("1 unit");
obj1.setImg_name(R.drawable.remdesiver);
holder.add(obj1);
PharmacyDataModel obj2 = new PharmacyDataModel();
obj2.setMed_name("PPE Kits");
obj2.setMfg_change("HealthBio Pharmaceuticals");
obj2.setPrice_change("800Rs.");
obj2.setQty_change("1 unit");
obj2.setImg_name(R.drawable.ppe);
holder.add(obj2);
PharmacyDataModel obj3 = new PharmacyDataModel();
obj3.setMed_name("Masks");
obj3.setMfg_change("HealthBio Pharmaceuticals");
obj3.setPrice_change("100Rs.");
obj3.setQty_change("1 unit");
obj3.setImg_name(R.drawable.masks);
holder.add(obj3);
PharmacyDataModel obj4 = new PharmacyDataModel();
obj4.setMed_name("Gloves");
obj4.setMfg_change("HealthBio Pharmaceuticals");
obj4.setPrice_change("50Rs.");
obj4.setQty_change("1 unit");
obj4.setImg_name(R.drawable.gloves);
holder.add(obj4);
PharmacyDataModel obj5 = new PharmacyDataModel();
obj5.setMed_name("Gluconate tablets");
obj5.setMfg_change("HealthBio Pharmaceuticals");
obj5.setPrice_change("100Rs.");
obj5.setQty_change("20 tablets");
obj5.setImg_name(R.drawable.gluconate);
holder.add(obj5);
PharmacyDataModel obj6 = new PharmacyDataModel();
obj6.setMed_name("Sugar Free");
obj6.setMfg_change("HealthBio Pharmaceuticals");
obj6.setPrice_change("250Rs.");
obj6.setQty_change("1 unit");
obj6.setImg_name(R.drawable.sugarfree);
holder.add(obj6);
PharmacyDataModel obj7 = new PharmacyDataModel();
obj7.setMed_name("Zincovit");
obj7.setMfg_change("HealthBio Pharmaceuticals");
obj7.setPrice_change("150Rs.");
obj7.setQty_change("1 unit");
obj7.setImg_name(R.drawable.zincovit);
holder.add(obj7);
return holder;
}
}
//Adapter class code
public class PharmacyAdapter extends RecyclerView.Adapter<PharmacyViewHolder> implements Filterable {
ArrayList<PharmacyDataModel> data;
ArrayList<PharmacyDataModel> datacopy;
private final Filter dataFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
ArrayList<PharmacyDataModel> filteredList = new ArrayList<>();
if (charSequence == null || charSequence.length() == 0) {
filteredList.addAll(datacopy);
} else {
String filterPattern = charSequence.toString().toLowerCase().trim();
for (PharmacyDataModel item : datacopy) {
if (item.getMed_name().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
data.clear();
data.addAll((ArrayList) filterResults.values);
notifyDataSetChanged();
}
};
public PharmacyAdapter(ArrayList<PharmacyDataModel> data) {
this.data = data;
datacopy = new ArrayList<>(data);
}
@NonNull
@Override
public PharmacyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.singlepharmacy_row, parent, false);
return new PharmacyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PharmacyViewHolder holder, int position) {
holder.textMedName.setText(data.get(position).getMed_name());
holder.textMedQty.setText(data.get(position).getQty_change());
holder.textMedPrice.setText(data.get(position).getPrice_change());
holder.textMedMfg.setText(data.get(position).getMfg_change());
holder.imageMed.setImageResource(data.get(position).getImg_name());
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public Filter getFilter() {
return dataFilter;
}
}
You need to convert a loop counter to string using str
, include the value in a list, use the multiplication operator to repeat the value in the list, and then use the string join
function to insert the spaces. Here's an example. If you want to start with 1 1 1
and not just 1
like I do here, then print(' '.join((i + 2) * [str(i)]))
instead of what is shown
>>> for i in range(1, 6):
... print(' '.join(i*[str(i)]))
...
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5