I want to get all items that contains the search input based on itemName. In c#, I can use lambda, but I could not find any references for android.
Here is the model class:
public class ModelItem {
public long itemId;
public String itemName;
public double price;
}
Here is my list:
public static ArrayList<ModelItem> items;
I will use the list to get the items. Thank you in advance.
Use below code
public void getAllItems(ArrayList<ModelItem> items, String searchItem) {
for(ModelItem item : items) {
if(item.getItemName().contains(searchItem)) {
// here you are getting item which matches inside your list
}
}
I think you have a listview with items. Now you want to filter them with a search string.
You have to implement Filterable in your custom adapter.
How to filter an adapter
First step, copy items into tempList
private ArrayList<ModelItem> items; // You have data into this list
private ArrayList<ModelItem> tempData = new ArrayList<>();
for (ModelItem item : items) {
tempData.add(item);
}
This is to filter items based on query
public void filter(String query) {
items.clear();
if (query.length() > 0) {
for (ModelItem currItem : tempData) {
// Add data into list, if item is having query string
if (currItem.getItemName().toLowerCase().contains(query)) {
mData.add(currItem);
}
}
} else {
// Adding all the items, if query is empty
for (ModelItem item : tempData) {
items.add(item);
}
}
notifyDataSetChanged(); // notify the changes, if you are using an adapter.
}
hey i got a example for your requirement in github, you need to use QueryTextListener in main class, then setFilter to adapter as given in example
please check this link:https://github.com/Wrdlbrnft/Searchable-RecyclerView-Demo
Related
class Item {
int id;
List<PriceDetails> priceDetails;
String itemName;
}
class PriceDetails {
int price;
}
I am getting multiple items in a JSON file. I am trying to filter priceDetails with empty price (not the items, just removing all the priceDetails in the list with empty price)
I am able to write Java code and its working as expected, but I don't know how to write using Java Streams. Can someone help me?
Thanks in advance.
Java Code :
public static List<Item> filterByEmptyPrice(List<Item> items) {
List<Item> result= new ArrayList<>();
for(int i=0;i<items.size();i++) {
List<PriceDetails> temp= new ArrayList<>();
for(int j=0;j<items.get(i).PriceDetails.size();j++) {
if(nonNull(items.get(i).PriceDetails) && nonNull(items.get(i).priceDetails.get(j).priceDetails.price)) {
temp.add(items.get(i).priceDetails.get(j));
}
}
items.get(i).priceDetails= temp;
result.add(items.get(i));
}
return result;
}
Your filterByEmptyTicketPrice() method doesn't compile with the Item and PriceDetails model you gave.
The correct loop based implementation would be:
public static List<Item> filterByEmptyTicketPrice(List<Item> items) {
List<Item> result = new ArrayList<>();
for (Item item : items) {
List<PriceDetails> temp = new ArrayList<>();
for (PriceDetails priceDetails : item.priceDetails) {
if (nonNull(priceDetails.price)) {
temp.add(priceDetails);
}
}
// bug: you mutate your method input here
item.priceDetails = temp;
result.add(item);
}
return result;
}
Also, as noted above, you're mutating the input items. The correct way to do this with streams and without mutating the input would be:
public static List<Item> filterByEmptyTicketPrice(List<Item> items) {
return items.stream()
.map(item -> new Item(filterPrices(item.priceDetails)))
.collect(Collectors.toList());
}
static List<PriceDetails> filterPrices(List<PriceDetails> priceDetailsList) {
return priceDetailsList
.stream()
.filter(priceDetails -> priceDetails.price != null)
.collect(Collectors.toList());
}
This example assumes you've added a new Item constructor such as:
public Item(List<PriceDetails> priceDetails) {
this.priceDetails = priceDetails;
}
As others mentioned, you should update the model to use getters to access priceDetails and price making them private in your Item and PriceDetails classes.
I tried this code and doesn`t return my inputed text.
This is my Addtocartitems.java :
This is my Downloadeditems.java :
This is my DownloadedListAdapter2.java :
This program has no error but nothings happened.
Can anybody help me please? Thanks in advance!
Create the list variavble in adapter class
List<DownloadedItems> downloadedItemsList= new ArrayList<>();
inside your constructor please call write down this
public DownloadListAdapter(Context context, int resource, ArrayList<DownloadedItems> object){
super(context,resource,object);
this.mcontext= context;
this.mResource=resource;
this.downloadedItemsList= object;
}
create the method in adapter which gets the list
public void setData(List<DownloadedItems> modelList) {
this.downloadedItemsList= modelList;
notifyDataSetChanged();
}
Now call the filter method with new filtered list and pass that list to adapter method
private void filter(String text) {
List<DownloadedItems> filteredList = new ArrayList<DownloadedItems>();
for (DownloadedItems la :langList) {
if (la.langName.toLowerCase().startsWith(text.toLowerCase())) {
filteredList.add(la);
}
adapter.setData(filteredList);
}
}
You can change the method .startsWith to .Contains as of your requirement. This method returns the items which starts with the entered string. langList is the initial list which i have passed already to the adapter to present.
la.langName.toLowerCase().startsWith
this is the filter query , the item which i am using to compare to filter. I am searching the language name from the list. So you can modify according to your requirement.and call the method filter from
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
filter(newText)
return true;
}
});
The code looks like this.
public class Album {
public String currentTitle;
public HashMap<String, List<Music>> albumList = new HashMap<String, List<Music>>();
//setting the album's title
public Album(String albumTitle) {
this.currentTitle = albumTitle; //represents object's name
albumList.put(currentTitle, null);
}
//add music to album
public void addMusicToThis(Music music) {
//only if value is empty
if(albumList.get(currentTitle) == null) {
albumList.put(currentTitle, new ArrayList<Music>());
}
albumList.get(currentTitle).add(music);
}
public void printMusicList() {
}
}
and I want to print all values for the specific album, like
Album album = new Album("Test1");
Album album2 = new Album("Test2");
album.addMusicToThis(something); //this code works fine
album2.addMusicToThis(something2);
album.printMusicList(); //maybe "something"
album2.printMusicList(); //maybe "something2"
but the hashMap's values are all set to List, and I can't find the way to print the musics out.
And assume that music's name is all set.
You just get the list for a particular string, and iterate it
for(Music m : albumList.get(this.currentTitle)) {
System.out.println(m.getName());
}
It's not really clear why you're using a Hashmap, though. Your key can never change.
You must iterate over the obtained list and print the individual entries
In Java 8 you can,
albumList.get(currentTitle).forEach((music) -> System.out.println(musice.getRequiredDetails)})
You can call albumList.entrySet() which is actually iterable, traverse it and print it however you like
I think you should add the albumTitle as an argument of the printMusicList function.
For example
public void printMusicList(String albumTitle) {
List<Music> musics = albumList.get(albumTitle);
for (Music music : musics) {
System.out.println(music);
}
}
or if you want to print it all
public void printMusicList() {
Set<String> keys = albumList.keySet();
for (String key : keys) {
List<Music> musics = albumList.get(key);
for (Music music : musics) {
System.out.println(music);
}
}
}
I have a class called persone (peoples), it's just an arraylist of object persona (person).
I want to use this the object persone for populate a JComboBox.
I've read many post, and I've understood that I've to use DefaultComboBoxModel(E[] items), but, of course, I've missed something. I made some mistake. Can I have an example ? And how to set or get the selected item?
This is my class:
public class Persone {
private ArrayList<Persona> el = new ArrayList<Persona>();
public Persone() {
}
public ArrayList<Persona> getEl() {
return el;
}
public void setEl(ArrayList<Persona> el) {
this.el = el;
}
public boolean delPersonaFromPersone(Persona persona) {
return this.el.remove(persona);
}
public boolean addPersonaToPersone(Persona persona) {
return this.el.add(persona);
}
public boolean substPersonaInPersone(Persona persona, Persona withPersona ) {
if ( !this.el.remove(persona))
return false;
return this.el.add(persona);
}
#Override
public String toString() {
return "Persone [el=" + el + "]";
}
}
You can't add an Object containing an ArrayList to a combo box.
Instead you need to add individual Persona object to the combo box.
Then you would need to provide a custom renderer to display the Persona object.
Check out Combo Box With Custom Renderer for more information and examples on how to do this.
I've found my mistake (some bad assignment).
For use in JComboBox, I've made a new array from ArrayList.
Here my code:
JComboBox<Persona> cbResponsabile = new JComboBox<Persona>();
Persona[] array = persone.getEl().toArray(new Persona[persone.getEl().size()]);
cbResponsabile.setModel(new DefaultComboBoxModel(array));
contentPanel.add(cbResponsabile);
// .....
// assignment
// persona is an element of array
cbResponsabile.setSelectedItem(persona);
I have a ListView and ListView adapter. I am adding objects to the adapter but I only want to add one row with an object that contains a certain String. This is my code but it does not work:
public static List<FriendsVideoLVModel> list = new ArrayList<FriendsVideoLVModel>();
#Override
public void add(FriendsVideoLVModel obj) {
super.add(obj);
for (int i=0; i <list.size(); i++) {
if (!obj.eventTitle.equals(list.get(i).eventTitle)) {
list.add(obj);
notifyDataSetChanged();
}
}
}
Please help. The logic looks fine to me but it just does not work. Nothing is in fact added.