I have an array list that displays a list of companies. Each of these companies is having corresponding town names. Further, I am creating buttons for each of the town names such that when I click on any of the town names, the ArrayList should be filtered and only the companies with that town name should be displayed.
The buttons are like,
stringList.add(tempList.get(n).getTownName());
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 60));
btnTag.setText(stringList.get(k));
btnTag.setBackgroundResource(R.drawable.alpha_button_selector);
btnTag.setClickable(true);
townLayout.addView(btnTag);
On clicking the button, I am fetching the text in the button as it displays the town name and then I am calling the method to filter the data,
btnTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CharSequence name = btnTag.getText();
locationFilter(name);
}
});
For filtering the data, I am creating a new ArrayList to store the filtered data such as,
ArrayList<CompanySearchResult> filtered = new ArrayList<CompanySearchResult>();
for (CompanySearchResult town : tempList) {
if (town.getTownName().equals(name)) {
filtered.add(town);
}
}
tempList.clear();
tempList.addAll(filtered);
listAdapter.notifyDataSetChanged();
The data is getting filtered properly, but my issue here is when I filter the data for the first time, my tempList(which is the main ArrayList containing the data) gets updated by the contents of the filtered ArrayList. Then when I click on the second button with another town name and try to filter that, it tries to filter the tempList which is already updated with the filtered data and the result displayed is blank.
Thus, how can I populate my tempList with the original data again each time a button with the town name is being clicked and then filter it out in a similar manner.
I have researched on this but couldn't find anything relevant. I have also tried the solution mentioned in the link:
android filter custom array adapter and bring back old items again
How can i take a backup of an ArrayList in Java after i call .clear()?
However, it didn't help. can anyone please help on how can I achieve this?