Reputation: 1164
In my android application I have dynamically created spinners and i want to get selected value of dynamically created spinner and I am doing like below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answermultitext);
String Title = "";
for (int count = 0; count < DropDown.length; count++) {
Title = DropDown[count].split(":")[0];
String DValue = DropDown[count].split(":")[1];
for (int i = 0; i < arr.length; i++) {
if (DValue.contains(arr[i])) {
DValue = DValue.replace(arr[i], "");
}
if(Title.contains(arr[i]))
{
Title = Title.replace(arr[i], "");
}
}
String[] data = DValue.split(",");
///Creating Text View
tv[count] = new TextView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) LayoutParams.WRAP_CONTENT,
(int) LayoutParams.WRAP_CONTENT);
params.leftMargin = 25;
params.topMargin = (count + 1) * (75 + add);
tv[count].setText(Title);
tv[count].setTextSize((float) 20);
tv[count].setPadding(5, 15, 5, 15);
tv[count].setLayoutParams(params);
layout.addView(tv[count]);
///Creating Spinner
Sp[count] = new Spinner(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
(int) LayoutParams.WRAP_CONTENT,
(int) LayoutParams.WRAP_CONTENT);
param.leftMargin = 25;
param.topMargin = (count + 1) * (100 + add1);
Sp[count].setPadding(5, 23, 5, 5);
Sp[count].setLayoutParams(param);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Sp[count].setAdapter(adapter);
Sp[count].setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// Get select item
int sid = Sp[count].getSelectedItemPosition();
// spinnerDropDown.getSelectedItem().toString();
Toast.makeText(getBaseContext(),
"You have selected : " + DropDown[sid],
Toast.LENGTH_SHORT).show();
}
// drop_SelectedVal
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
layout.addView(Sp[count]);
}
}
But I am getting below error in onItemSelected()
method at this line int sid = Sp[count].getSelectedItemPosition();
Cannot refer to a non-final variable count inside an inner class defined in a different method
And when I add final
with the count
vaiable the above error is remove and I am getting below error at this line for(int count = 0; count < DropDown.length; count++)
The final local variable count cannot be assigned. It must be blank and not using a compound assignment
kindly suggest me how can I resolve it.
waiting for reply.
Thanks
Upvotes: 0
Views: 1105
Reputation: 1523
First of all about the error : the error is because you are accessing non-final Spinner object Sp[count] in inner class. So changing count to final will create another error but the previous problem is still not solved. Its just suppressed for time being when you will solve the error with count it will come again.
How to solve ?
First remove the final you added with the count.
Second if you want only the position of the selected item in the spinner you should do the following
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
int selectedItem = position;
}
Third If you want to get the String selected you should do the following :
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
String s = (String) parent.getItemAtPosition(position);
// here's the change
}
Upvotes: 0
Reputation: 1033
you can use the
int position
on onItemSelected.
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
int hereIsYourSelectedItem = position;
}
Upvotes: 1