I am trying to get the Id of a button when it is clicked. I have implemented a switch to get the Id(stored as resourceName
) which works as System.out.println
works. However when I use resourceName
to get my data, there is an error "cannot resolve symbol
'.
public class NavigationActivity extends AppCompatActivity {
ImageButton Apple;
ImageButton Asus;
FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference details = db.collection("devices");
ArrayAdapter arrayAdapter;
TextView device_name;
TextView device_description;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
public void deviceList (View V){
Apple = (ImageButton) findViewById(R.id.Apple);
Asus = (ImageButton) findViewById(R.id.Asus);
final ArrayList<String> deviceNames;
deviceNames = new ArrayList<>();
final ArrayList<String> selectedDevice;
selectedDevice = new ArrayList<>();
int iddd = V.getId();
switch (iddd){
case R.id.Asus:
case R.id.Apple:
String resourceName = getResources().getResourceEntryName(iddd);
System.out.println(resourceName);
break;
}
db.collection("devices")
.whereEqualTo("brand", resourceName)
.get()
....
});
};
}
The variable is defined in a very small scope within that switch case. If it needs a larger scope, define it outside of the
switch
:Of course, it's up to you to determine what would be a sensible default value or what to do with that default value, or any other way in which you want to handle the case where your
switch
never assigns the intended value to the variable.