1

I have an Android app that uses Firebase and I am having an error with a query.

In my class file I am defining the search and I am getting:

error: cannot find symbol variable query

Here is the code from my class file:

public class SearchFragment extends Fragment {

    private static final String TAG = "SearchFragment";
    private static final int NUM_GRID_COLUMNS = 3;
    private static final int GRID_ITEM_MARGIN = 5;

    //widgets
    private ImageView mSearchButton;
    private EditText mTitleET;
    private FrameLayout mFrameLayout;


    //vars
    private String mElasticSearchPassword;
    private String mPrefCity;
    private String mPrefStateProv;
    private String mPrefCountry;
    private ArrayList<Post> mPosts;
    private RecyclerView mRecyclerView;
    private PostListAdapter mAdapter;
    private String queryText;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //View view = inflater.inflate(R.layout.fragment_search, container, false);
        View view = inflater.inflate(R.layout.fragment_search2, container, false);
        //mFilters = (ImageView) view.findViewById(R.id.ic_search);
        mSearchButton = (ImageView) view.findViewById(R.id.search_button);
        mTitleET = (EditText) view.findViewById(R.id.input_title);
        //mSearchText = (EditText) view.findViewById(R.id.input_search);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mFrameLayout = (FrameLayout) view.findViewById(R.id.container);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();


        mSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Query query = reference.child("posts/").orderByChild("title").equalTo(mTitleET.getText().toString().trim()).
                        query.addListenerForSingleValueEvent(new ValueEventListener()
                {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            // dataSnapshot is the "issue" node with all children with id 0
                            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                // do something with the individual "issues"
                                Log.i("TAG", ds.getValue().toString());
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.i("ERRRRRROORRR", databaseError.getMessage() + ", so....let´s take a coffee");

                    }
                });

            }
        });    
    }

    private void setupPostsList(){
        RecyclerViewMargin itemDecorator = new RecyclerViewMargin(GRID_ITEM_MARGIN, NUM_GRID_COLUMNS);
        mRecyclerView.addItemDecoration(itemDecorator);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), NUM_GRID_COLUMNS);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mAdapter = new PostListAdapter(getActivity(), mPosts);
        mRecyclerView.setAdapter(mAdapter);
    }


}

It is specifically the following line(s) that appear to be the problem (see attached pic): enter image description here What is causing this error and what is the solution for this error?

Onik
  • 19,396
  • 14
  • 68
  • 91
ironmantis7x
  • 807
  • 2
  • 23
  • 58
  • 3
    You have a dot (`.`) after `trim()).` – Onik Nov 07 '18 at 12:53
  • No need to use query keyword before addListenerForSingleValueEvent or other way is to replace Dot (.) after trim()) with Semicolon (;) – ShehrozEK Nov 07 '18 at 13:01
  • This is a Java compilation error, which is often caused by a typo. See [the answer at What does a “Cannot find symbol” compilation error mean?](https://stackoverflow.com/a/25706217/2754146). – Grimthorr Nov 07 '18 at 13:39

1 Answers1

1

You have a redundant dot (.) after the trim() call at

Query query = reference.child("posts/").orderByChild("title").equalTo(mTitleET.getText().toString().trim()). // <- this one

which is apparently a typo.

Onik
  • 19,396
  • 14
  • 68
  • 91