I have developed the following for loop to set the data in a drop down list. This works perfectly if I use a number on the .get() method to select and compare which item has been clicked, but obviously this is useless with a set integer value.
I’m getting the error that the ‘a’ variable cannot be resolved to a variable.
I’m really not sure why though?
Here’s the code:
List<String> list = new ArrayList<String>();
list.add("-");
list.add("Medical");
list.add("Business");
list.add("Family");
list.add("Other");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
editTime.setAdapter(dataAdapter);
for(int a = 0; a < list.size(); a ++);
{
// Error on the 'a' variable - cannot be resolved to a variable.
if(typeReturned == list.get(a));
{
// Error on the 'a' variable - cannot be resolved to a variable.
editTime.setSelection(a);
}
}
You have an unnecessary semicolon after your
forloop:If you don’t delete it, it is the same as writing:
You have the same mistake after your
ifcondition. You have to get rid of that semicolon, too.Also, I think
typeReturnedis aString, and you are comparing it with==. That is a bad idea. You have to use.equals(), if you want to check if both strings have the same content, see:How do I compare Strings in Java