I know the topic sounds like a dupplicate, but I am not trying to debug this issue here but to get around it.
What my program is supposed to do is : For every file there is in a folder, create a new button. If you click on this button, put in the intent extra the name of the folder you've just entered.
File directory = new File(prefs.getString("path",null));
File[] files = directory.listFiles();
for (File file :files){
if(file.isDirectory()) {
buttons.add(new Button(this));
Button button = buttons.get(buttons.size() - 1);
String fileName = file.getName();
button.setText(fileName);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(currentActivity, EOChoiceActivity.class);
intent.putExtra("fileExtension",fileName);
startActivity(intent);
}
});
layout.addView(button);
}
}
Now the problem I have is that the "fileName" variable needs to be put in final, due to Java giving the pointer of the object and not the value itself. But if I do so, only the last fileName selected will be sent for any button.