I have implemented a cardview along with a recyclerview that fetch data from firebase. I want to implement onclick methods to cards that goes for different activities. How can i add on click method for the cards? Here is my mainactivity.
public class English extends AppCompatActivity {
ViewHolder apt;
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_english);
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("English");
}
private void firebaseSearch(String searchText){
Query firebaseSearchQuery=mRef.orderByChild("artist").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerAdapter<englishacti, ViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<englishacti, ViewHolder>(englishacti.class,R.layout.row,ViewHolder.class,firebaseSearchQuery) {
#Override
protected void populateViewHolder(ViewHolder holder, englishacti model, int position) {
holder.setDetails(getApplicationContext(),model.getAlbum(),model.getDescription(),model.getImage(),model.getArtist());
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<englishacti,ViewHolder> firebaseRecyclerAdapter;
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<englishacti, ViewHolder>(englishacti.class,R.layout.row,ViewHolder.class,mRef) {
#Override
protected void populateViewHolder(ViewHolder viewHolder, englishacti model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getAlbum(), model.getDescription(), model.getImage(), model.getArtist());
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
firebaseSearch(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
firebaseSearch(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if (id==R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
}
please take a look at my snipshed code, in my case i use this way to implement setonlclick in recycleview item and send value to other activity
holder.setName(userName);
holder.setUserImage(userThumb, getContext());
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent chatIntent = new Intent(getContext(), Tampung_chatActivity.class);
chatIntent.putExtra("user_id", list_user_id);
chatIntent.putExtra("user_name", userName);
startActivity(chatIntent);
}
});
full code you can see at here :
full snipshed code implement onclick with recycleview and firebase
Related
when i fetch data from firebase to listview with a itemclicklistener and its working fine,
But when i add a searchview to the listview, Searchview will fetch the itementer image description here but the itemclicklistener showing a wrong result (its showing its position), Please help.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btMenu = findViewById(R.id.btMenu);
sr_txt = findViewById(R.id.sr_txt);
listView = findViewById(R.id.listView);
databaseReference = FirebaseDatabase.getInstance().getReference("dreamapp");
dream1 = new Dream();
title_list = new ArrayList<>();
answer_list = new ArrayList<>();
arrayAdapter = new ArrayAdapter<>(this,R.layout.item,R.id.item,title_list);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot d:snapshot.getChildren())
{
dream1 = d.getValue(Dream.class);
title_list.add(dream1.getTitle());
answer_list.add(dream1.getAnswer());
}
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i,long id) {
Intent intent = new Intent(MainActivity.this, answer.class);
String p=answer_list.get(i);
intent.putExtra("answer",p);
startActivity(intent);
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
sr_txt.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
MainActivity.this.arrayAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
MainActivity.this.arrayAdapter.getFilter().filter(newText);
return false;
}
});
}
And i have passed the answerlist values from firebase to another intent which is given below , where the result is fetching as position number from database which i need as itemsposition.
package com.example.dreamapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class answer extends AppCompatActivity {
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
textView1 = findViewById(R.id.answertext);
String answertxt = getIntent().getStringExtra("answer");
textView1.setText(answertxt);
}
}
Hi i have found the solution for this, When i added one more arrayadapter and listview , then i used it it in the searchview then it worked, it get the results if i search the exact words in it,
sr_txt.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
MainActivity.this.arrayAdapter.getFilter().filter(query);
MainActivity.this.arrayAdapter2.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
MainActivity.this.arrayAdapter.getFilter().filter(newText);
MainActivity.this.arrayAdapter2.getFilter().filter(newText);
return false;
}
});
And SearchView
sr_txt.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
MainActivity.this.arrayAdapter.getFilter().filter(query);
MainActivity.this.arrayAdapter2.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
MainActivity.this.arrayAdapter.getFilter().filter(newText);
MainActivity.this.arrayAdapter2.getFilter().filter(newText);
return false;
}
});
But still i face an issue with searchView, When i search it searches the entire characters from the array, i need a solution if i could search only five characters from it , it will work fine.
Hi I am using Firebase Database with search query. I am also using offline method to store data from firebase offline. But after implementing searchview when I am opening the page it is showing blank. Data is coming after clicking the search icon. How to solve this? My application is on the playstore. It is the Link of the Application
And Here is my search query Code
private void firebaseUserSearch(String searchText) {
Query firebaseSearchQuery = mUserDatabase.orderByChild("Title").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerAdapter<Blog,BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.bank_row,
BlogViewHolder.class,
firebaseSearchQuery
) {
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, final Blog model, int position) {
final String fav_key=getRef(position).getKey();
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getExample());
viewHolder.setFavourite(fav_key);
viewHolder.favourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mfavourite=true;
databasefavourite.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(mfavourite){
if (dataSnapshot.child(fav_key).hasChild("Title")){
databasefavourite.child(fav_key).child("Title").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Title").setValue(model.getTitle());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Description")){
databasefavourite.child(fav_key).child("Description").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Description").setValue(model.getDescription());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Example")){
databasefavourite.child(fav_key).child("Example").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Example").setValue(model.getExample());
mfavourite = false;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
It is my code for onStart()
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Blog,Idioms.BlogViewHolder>firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Blog, Idioms.BlogViewHolder>
(Blog.class,R.layout.bank_row,Idioms.BlogViewHolder.class,mUserDatabase) {
#Override
protected void populateViewHolder(Idioms.BlogViewHolder viewHolder, final Blog model, int position) {
final String fav_key=getRef(position).getKey();
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getExample());
viewHolder.setFavourite(fav_key);
viewHolder.favourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mfavourite=true;
databasefavourite.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(mfavourite){
if (dataSnapshot.child(fav_key).hasChild("Title")){
databasefavourite.child(fav_key).child("Title").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Title").setValue(model.getTitle());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Description")){
databasefavourite.child(fav_key).child("Description").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Description").setValue(model.getDescription());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Example")){
databasefavourite.child(fav_key).child("Example").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Example").setValue(model.getExample());
mfavourite = false;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
And it is my search action code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
firebaseUserSearch(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
firebaseUserSearch(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
I can able to retrieve data from Firebase to App through RecyclerView. But when I am trying to add Search view its not working.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
final SearchView searchView =(SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menusearch));
SearchManager searchManager =(SearchManager) getSystemService(SEARCH_SERVICE); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName());
//searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//SearchText.setText(newText);
Query Q = mDatabase.child("Recipees").orderByChild("cusine").startAt(newText).endAt("");
FirebaseRecyclerAdapter<ImageRetrieve, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ImageRetrieve, BlogViewHolder>(
ImageRetrieve.class,
R.layout.recipe_row,
BlogViewHolder.class,
Q
) {
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, ImageRetrieve model, int position) {
viewHolder.setCusine(model.getCusine());
viewHolder.setCatagory(model.getCatagory());
viewHolder.setRecipe(model.getRecipe());
viewHolder.setUrl(getApplicationContext(), model.getUrl());
}
};
mRecipeList.setAdapter(firebaseRecyclerAdapter);
return false;
}
});
return true;
}
Try this it may help :
Query Q = mDatabase.child("Recipees").orderByChild("cusine").startAt(newText).endAt(newText+"\uf8ff");
So that mean query have wrong.
Try This:-->
mDatabase.child("Recipees").orderByChild("cusine").equalTo(newText);
Try the code below, it might be helpful:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
// if searchView is not empty
if(!text.isEmpty()){
// call seearch method if user starts typing
search(text);
}
//else
else{
// set TextField empty
search("");
}
return true;
}
});
// method to perform search
private void search(String newText)
{
ArrayList<> searchList = new ArrayList<>();
// creates a query to initiate the search
Query Q = mDatabase.child("Recipees")
.orderByChild("cusine").startAt(newText).endAt(newText + "\uf8ff");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChildren()){
//clears the arrayList
searchList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
final ImageRetrieve imageRetrieve =
snapshot.getValue(ImageRetrieve.class);
//adds the rooms searched to the arrayList
searchList.add(imageRetrieve);
}
//NB: recyclerView is the view object in your activity that populates data
FirebaseRecyclerAdapter firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter(yourActivity.this,searchList);
recyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// display message if error occurs
}
});
}
In my software I need a Query, how do I Query 2 information from the Database?? I need the "uid" and "book", check the link for watching my Database: https://i.stack.imgur.com/9TSt6.png
listWN = (RecyclerView) findViewById(R.id.listReading);
listWN.setLayoutManager(new LinearLayoutManager(this));
bookYouAreIn = getIntent().getStringExtra("book");
firebaseauth = FirebaseAuth.getInstance();
String currentUserId = firebaseauth.getCurrentUser().getUid();
mDatabase = FirebaseDatabase.getInstance().getReference("Data");
mQuery = mDatabase.orderByChild("book").equalTo(bookYouAreIn); **---> HOW DO I ADD HERE: AND mDatabase.orderByChild("uid").equalTo(firebaseauth.getCurrentUser().getUid());**
mDatabase.keepSynced(true);
if(!firebaseauth.getCurrentUser().getUid().equals(currentUserId)){
startActivity(new Intent(WordNote.this,LoginActivity.class));
finish();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_secondmenu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_addWords){
Intent postIntent = new Intent(WordNote.this, PostActivity.class);
postIntent.putExtra("bookToPost", bookYouAreIn);
startActivity(postIntent);
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<MyReading, MyReadingViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <MyReading, MyReadingViewHolder>
(
MyReading.class,
R.layout.list_reading,
MyReadingViewHolder.class,
mQuery )
{
#Override
protected void populateViewHolder(final MyReadingViewHolder viewHolder, final MyReading model, final int position) {
final String wordNote_key = getRef(position).getKey();
viewHolder.setWord(model.getWord());
viewHolder.setNote(model.getNote());
mDatabase.child(wordNote_key).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String user = (String) dataSnapshot.child("uid").getValue();
String bookNow = (String) dataSnapshot.child("book").getValue();
if(firebaseauth.getCurrentUser().getUid().equals(user)){
mDatabase.child(wordNote_key).orderByChild("uid").equals(user);
}
else {
viewHolder.nView.setVisibility(View.GONE); **--> IT DOESN'T GO AWAY, IT KEEPS STAYING AS A BLANK VIEW**
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.nView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent singleIntent = new Intent(WordNote.this, HandleWordNote.class);
singleIntent.putExtra("listWords", wordNote_key);
startActivity(singleIntent);
}
});
}
};
listWN.setAdapter(firebaseRecyclerAdapter);
See my answer from this post.
In your case, the field would be: uid_book: "OKtUxSleCGbU0F_Pasqua".
Hope it helps.
I am right now fetching movies to my MainActivity.
I need infinite scroll on my RecycleView inside MainActivity.
Can someone tell me where to set code so I can have infinite scroll?
If you need more data, tell me.
Thank you.
MainActivity Code is bellow:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MoviesAdapter mAdapter;
#Override
protected void onResume() {
SharedPreferences sharedPref = getBaseContext().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
String order = sharedPref.getString("order", "popular");
if (order.equals("popular")) {
getPopularMovies();
} else {
getTopRatedMovies();
}
super.onResume();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// uci i postaviti order -> popular
Log.d("NESTO", "NESTO-onCreate");
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
mAdapter = new MoviesAdapter(MainActivity.this);
mRecyclerView.setAdapter(mAdapter);
}
private void getTopRatedMovies() {
RestAdapter.getMovieService().getTopRatedMovies(new Callback<MovieResult>() {
#Override
public void success(MovieResult movieResult, Response response) {
mAdapter.setMovieList(movieResult.getResults());
}
#Override
public void failure(RetrofitError error) {
error.printStackTrace();
}
});
}
private void getPopularMovies() {
RestAdapter.getMovieService().getPopularMovies(new Callback<MovieResult>() {
#Override
public void success(MovieResult movieResult, Response response) {
mAdapter.setMovieList(movieResult.getResults());
}
#Override
public void failure(RetrofitError error) {
error.printStackTrace();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class MovieViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public MovieViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
}