Reputation: 3122
I've made a question (deleted) previously but i think it were very confused and hard to understand, so this question is about the same problem, but simpler than before.
I made an example project with the same situation, there are the following items:
For each filtred item on my Set
, I make one new View to add on my main View. These new Views, has an OnClickListener
, that theoretically shows all items filtred.
The problem is when I click on any view, the result is the same.
My Code:
public class MainActivity extends Activity {
private List<Integer> myList = new ArrayList<Integer>();
private List<Integer> myFinalList;
private Set<Integer> setOfAallCodes = new HashSet<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList.add(1);
myList.add(1);
myList.add(1);
myList.add(2);
myList.add(2);
myList.add(2);
listItems();
}
public void listItems() {
myFinalList = new ArrayList<Integer>();
for (Integer element : myList) {
setOfAallCodes.add(element);
}
Object[] arrayOfAllCodes;
arrayOfAllCodes = setOfAallCodes.toArray();
for (Object i : arrayOfAllCodes) {
myFinalList.clear();
int actual = (Integer) i;
for (Integer element : myList) {
if (element == actual) {
myFinalList.add(element);
}
}
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.button_view, null);
Button btnTest = (Button) v.findViewById(R.id.mega_button);
View insertPoint = findViewById(R.id.conteudo);
((ViewGroup) insertPoint).addView(v);
btnTest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Result: " + myFinalList);
}
});
}
}
}
Result of both buttons is: [2, 2, 2]
What is wrong? What can I do to make the buttons show their results?
Upvotes: 2
Views: 191
Reputation: 3122
I did it!
Thanks for advices, I had to make some operations inside ClickListener.
My Final code:
public class MainActivity extends Activity {
private List<Integer> myList = new ArrayList<Integer>();
private Set<Integer> setOfAallCodes = new HashSet<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList.add(1);
myList.add(1);
myList.add(1);
myList.add(2);
myList.add(2);
myList.add(2);
listItems();
}
public void listItems() {
for (Integer element : myList) {
setOfAallCodes.add(element);
}
Object[] arrayOfAllCodes;
arrayOfAllCodes = setOfAallCodes.toArray();
for (Object i : arrayOfAllCodes) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.button_view, null);
Button btnTest = (Button) v.findViewById(R.id.mega_button);
View insertPoint = findViewById(R.id.conteudo);
((ViewGroup) insertPoint).addView(v);
final int atual = (Integer) i;
btnTest.setOnClickListener(new OnClickListener() {
List<Integer> myFinalList = new ArrayList<Integer>();
@Override
public void onClick(View v) {
for (Integer integer : myList) {
if (atual == integer) {
myFinalList.add(integer);
}
}
System.out.println(myFinalList);
myFinalList.clear();
}
});
}
}
}
Upvotes: 0
Reputation: 16128
I'll make a try. This will not be sufficient for all the issues:
You'll need one "myFinalList" per View, not one global one. Right now, all your buttons print the contents of one and the same instance of the list.
Please elaborate how your "filter" is supposed to be working. Did you expect the following output?
View 1 = [1,1,1], View 2 = [2,2,2]
Upvotes: 1