Reputation: 3
I'm using The Complete Idiots Guide to Android App Development to get me started. I've been struggling through the book and I've come accross a problem that I don't understand. I'm creating a search class to add a search function to the app. I'm getting multiple errors in eclipse and the one I don't understand the most is "Syntax error, insert "}" to complete ClassBody". I understand that Eclipse is telling me to close the class body, but it is closed. Just not where it's telling me to close it. Below where it's telling me to close it I have another method to enter.
Here is the code for the whole class.
package com.recipesapp;
import java.util.ArrayList;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ListView;
public class RecipeSearch extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState){
//super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_list);
Intent intent = getIntent();
if(Intent.ACTION_VIEW.equals(intent.getAction())){
//A search suggestion was clicked
Intent recipeIntent = new Intent(this, RecipeEntry.class);
recipeIntent.setData( intent.getData() );
startActivity(recipeIntent);
finish();
}
else if(Intent.ACTION_SEARCH.equals(intent.getAction())){
//The search was executed
String query = intent.getStringExtra(SearchManager.QUERY);
showRecipes(query);
}
}
private static final ArrayList<String> _RecipeSearchResults = new ArrayList<String>();
private ListView resultsView;
private void showRecipes(query){
SharedPreferences recipeNames = getSharedPreferences(MainMenu.RecipeNamesPref, RecipeEntry.MODE_WORLD_READABLE);
String[] recipeList= recipeNames.getString(MainMenu.RecipeNamesPref, null).split(",");
for(String recipe: recipeList){
if(recipe.contains(query))
ReicpeSearchResults.add(recipe);
resultsView=(ListView) findViewById(android.R.id.list);
resultsView.setAdapter(new ListViewAdapter(this));
resultsView.setTextFilterEnabled(true);
resultsView.setOnItemClickListener(this);
}
}
}
I know there are other errors, but I can't figure this one out. I've gone through and seen that I have all the curly brackets, open and closed. I don't understand why it wants to put in line 35, which is:
private ListView resultsView;
Also, it wants me to also delete the very last curly bracket. I would appreciate any help.
Upvotes: 0
Views: 1385
Reputation: 6635
private static final ArrayList<String> _RecipeSearchResults = new ArrayList<String>();
private ListView resultsView;
Both these two lines mentioned above are actually supposed to come before OnCreate
Where you are actually writing them. at that place every line is supposed to be inside some method.
@Override
public void onCreate(Bundle savedInstanceState){
//super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_list);
Intent intent = getIntent();
if(Intent.ACTION_VIEW.equals(intent.getAction())){
//A search suggestion was clicked
Intent recipeIntent = new Intent(this, RecipeEntry.class);
recipeIntent.setData( intent.getData() );
startActivity(recipeIntent);
finish();
}
else if(Intent.ACTION_SEARCH.equals(intent.getAction())){
//The search was executed
String query = intent.getStringExtra(SearchManager.QUERY);
showRecipes(query);
}
More Over you are supposed to define tthe Data Type of the "query"
parameter say String
here in this case
private void showRecipes(String query){
SharedPreferences recipeNames = getSharedPreferences(MainMenu.RecipeNamesPref, RecipeEntry.MODE_WORLD_READABLE);
String[] recipeList= recipeNames.getString(MainMenu.RecipeNamesPref, null).split(",");
for(String recipe: recipeList){
if(recipe.contains(query))
ReicpeSearchResults.add(recipe);
resultsView=(ListView) findViewById(android.R.id.list);
resultsView.setAdapter(new ListViewAdapter(this));
resultsView.setTextFilterEnabled(true);
resultsView.setOnItemClickListener(this);
}
}
Upvotes: 0
Reputation: 20155
This is not the problem with curly brace. You missed a type for your method parameter
A method parameter should have type
Change this
showRecipes(query)
to
this
showRecipes(String query)
Upvotes: 2
Reputation: 1385
Just move following line before onCreate function
private ListView resultsView;
Upvotes: 0
Reputation: 29642
Your method private void showRecipes(query)
does not contain a data type, that's why it is showing an error.
if you change it to something like this private void showRecipes(String query)
it will stop showing }
error.
Upvotes: 0