Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding search query #5

Closed
9john opened this issue Feb 13, 2018 · 3 comments
Closed

adding search query #5

9john opened this issue Feb 13, 2018 · 3 comments

Comments

@9john
Copy link

9john commented Feb 13, 2018

how to add search query for any keyword from title or description

@9john
Copy link
Author

9john commented Feb 13, 2018

can we go like this

public class MainActivity extends AppCompatActivity {

private EditText mSearchText;
private ImageButton mSearchbtn;

private RecyclerView mList;

//firebase
private DatabaseReference mContentRef;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSearchText = (EditText) findViewById(R.id.searchTextView);
    mSearchbtn = (ImageButton) findViewById(R.id.searchBtn);
    mList = (RecyclerView) findViewById(R.id.listView);
    mList.setHasFixedSize(true);
    mList.setLayoutManager( new LinearLayoutManager(this));

    //firebase database reference of content we are search
    mContentRef = FirebaseDatabase.getInstance().getReference().child("Content");

    mSearchbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //get the text from the edit text as string into a variable searchText
            String searchText = mSearchText.getText().toString();

            //pass the variable search text in the method searchText
            firebaseSearch(searchText);
        }
    });



}


//here we declear a method to carryout the search and display it in recycler view
//using firebase ui recycler adapater
private void firebaseSearch(String searchText) {


    //declear a firebase query to retrieve the search text,
    //start at the letter typed and end at the letter typed
    //
    Query searchQuery = mContentRef.orderByChild("title").startAt(searchText).endAt(searchText + "\uf8ff");

    /*
    * FirebaseRecycler add accept the model claa and view holder class
    * also we pass in four arguments:
    * the model class , the single item layout, the view Holder class and the search query
     */
    FirebaseRecyclerAdapter<Search, ContentViewHolder> fireAdapter = new FirebaseRecyclerAdapter<Search, ContentViewHolder>(
            Search.class,
            R.layout.item_layout,
            ContentViewHolder.class,
            searchQuery


    ) {
        @Override
        protected void populateViewHolder(ContentViewHolder viewHolder, Search model, int position) {

            viewHolder.setContents(model.getTitle(),model.getDesc());
        }
    };

    mList.setAdapter(fireAdapter);
}


//View holder class
//we set the contents to the view widget

public static class ContentViewHolder extends RecyclerView.ViewHolder{
    View mView;
    public ContentViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }
    public void setContents(String title,String desc){
        TextView titleText = (TextView) mView.findViewById(R.id.titleText);
        TextView descText = (TextView) mView.findViewById(R.id.DescText);

        titleText.setText(title);
        descText.setText(desc);
    }


}

}

and search.class as

public class Search {

private String title;
private  String desc;

public Search() {
}

public Search(String title, String desc) {

    this.title = title;
    this.desc = desc;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

@hafieyz
Copy link

hafieyz commented Apr 26, 2018

How about if using Searchview and display result on recylerview?..
example in stackoverflow : https://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview

im bit confuse to apply on this app..hopefully someone expert can help us.. 😀

@Batishev-Rozdoum
Copy link
Contributor

Hello @hafieyz and @9john , thank you for interesting in our project. You can create a pull requests if you want to add the search feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants