I have an extremely long list of items (over 200) and a database that displays the list. I have an on click event that will compare that if the first item is an "apple" then when you click it, facts come up about an "apple". The problem is that this list isn't a SET list, meaning the the word "apple" could be in the 1st spot or it could be in the 18th spot.
I started doing an if statement that compares like this:
case 0:
if (text.equals("apple")) {
[show facts about apple]
} else if (text.equals("orange")) {
[show facts about orange]
//this would continue on to compare the ENTIRE LIST (about 500 lines per case)
break;
The problem is that i got an error that states:
The code of method onListItemClick(ListView, View, int, long) is exceeding the 65535 bytes limit
There must be an easier way to do this, right?
First of all, to just solve your "method too long" problem, there are several ways.
#1 move all your description into strings.xml.
if (text.equals("apple")) {
result = getResources().getString(R.string.apple_description);
}
#2 move your if-else into a separated method.
case 0:
mydescription = getDescription(text); // getDescription() is the big if-else you have
BUT..... it is still very bad to code in such a way.
Please consider following:
#1 Create a HashMap for name and description.
#2 In your list adapter, set a tag as indicator.
view.setTag("apple");
#3 In your onListItemClick, read this tag and get description.
String text = view.getTag();
String description = myhashmap.get(text).toString();
// If your hashmap is mapping name and string resource id then:
String description = getResources().getString(Integer.parseInt(myhashmap.get(text)));