I've been searching about autocomplete textview but I can't seem figure out how can i implement it in firebase recycler adapter ? I saw this thread: How can I load an AutoCompleteTextView from a list of Firebase data?
But I don't know how I can implement it in my program .
Here's my portion of my code ,
private void firebaseUserSearch(String searchText) {
String spinnerSearchs=spinnerSearch.getSelectedItem().toString();
Toast.makeText(getActivity(), "Started Search", Toast.LENGTH_LONG).show();
if(spinnerSearchs.equals("Book Title")){
Query firebaseQuerySearch = mBookDatabase.orderByChild("bookTitle").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerOptions bookOptions= new FirebaseRecyclerOptions.Builder<Book>().setQuery(firebaseQuerySearch,Book.class).build();
mBook = new FirebaseRecyclerAdapter<Book, UsersViewHolder>(bookOptions) {
#Override
protected void onBindViewHolder(final UsersViewHolder holder, final int position,final Book model) {
holder.setDetails(model.getBookId(),model.getBookTitle(),model.getAuthorName());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void OnClick(View view, int position, boolean isLongClick) {
Intent intent = new Intent(getActivity().getBaseContext(),Borrow.class);
intent.putExtra(BOOK_ID,model.getBookId());
getActivity().startActivity(intent);
}
});
holder.setIsRecyclable(false);
}
#Override
public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_layout,parent,false);
return new UsersViewHolder(view,bookList);
}
};
mSearchList.setAdapter(mBook);
} else if(spinnerSearchs.equals("Author")){
Query firebaseQuerySearch = mBookDatabase.orderByChild("authorName").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerOptions bookOptions= new FirebaseRecyclerOptions.Builder<Book>().setQuery(firebaseQuerySearch,Book.class).build();
mBook = new FirebaseRecyclerAdapter<Book, UsersViewHolder>(bookOptions) {
#Override
protected void onBindViewHolder(final UsersViewHolder holder, final int position,final Book model) {
holder.setDetails(model.getBookId(),model.getBookTitle(),model.getAuthorName());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void OnClick(View view, int position, boolean isLongClick) {
Intent intent = new Intent(getActivity().getBaseContext(),Borrow.class);
intent.putExtra(BOOK_ID,model.getBookId());
getActivity().startActivity(intent);
}
});
holder.setIsRecyclable(false);
}
#Override
public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_layout,parent,false);
return new UsersViewHolder(view,bookList);
}
};
mSearchList.setAdapter(mBook);
}
mBook.startListening();
}
Related
Inside my Firestore Cloud-Storage I got this Boolean-field inside Products/Productid/: "isOffer".
I want to change the color of the TextView-String "mPrice" inside my RecyclerView ProductItems, regarding if "isOffer" is true or false, but I have no Idea how to handle that.
Method createRecycler():
public void createRecycler(){
mRecyclerView = findViewById(R.id.product_selection_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
Query query = current_productRef.whereEqualTo("isAvailable", true);
FirestoreRecyclerOptions<product_selection_Item> options = new FirestoreRecyclerOptions.Builder<product_selection_Item>()
.setQuery(query, product_selection_Item.class)
.build();
mAdapter = new product_selection_Adapter(options);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new product_selection_Adapter.OnItemClickListener() {
#Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(product_selection.this, product_fill.class);
intent.putExtra("Product_ID", id);
startActivity(intent);
}
});
}
My Adapter for the Recycler is pretty basic:
public class product_selection_Adapter extends FirestoreRecyclerAdapter<product_selection_Item, product_selection_Adapter.product_selection_ViewHolder> {
private OnItemClickListener mListener;
public interface OnItemClickListener {
void OnItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public class product_selection_ViewHolder extends RecyclerView.ViewHolder {
public TextView mProductname;
public TextView mFat_Value;
public TextView mPrice;
public product_selection_ViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mProductname = itemView.findViewById(R.id.product_selection_name);
mFat_Value = itemView.findViewById(R.id.product_selection_fat);
mPrice = itemView.findViewById(R.id.product_selection_price);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.OnItemClick(getSnapshots().getSnapshot(position), position);
}
}
}
});
}
}
public product_selection_Adapter(#NonNull FirestoreRecyclerOptions<product_selection_Item> options) {
super(options);
}
#NonNull
#Override
public product_selection_ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_selection_item, parent, false);
product_selection_ViewHolder ViewHolder = new product_selection_ViewHolder(v, mListener);
return ViewHolder;
}
#SuppressLint("SetTextI18n")
#Override
protected void onBindViewHolder(#NonNull product_selection_ViewHolder holder, int position, #NonNull product_selection_Item model) {
holder.mProductname.setText(model.getmProductname());
holder.mFat_Value.setText(String.valueOf(model.getmFat_Value() + "% Fett"));
holder.mPrice.setText(String.valueOf(model.getmPrice().get(0) + " €"));
}
}
Can you help me out here?
Thank you in Advance!
I solved it.
I needed to create a new Attribute inside my Product_Selection_Item - Class "isOffer" with the getter "getisOffer".
After that, inside the Adapter I simply created a new boolean variable "isOffer" which gets the value of the current bool. Just like:
protected void onBindViewHolder(#NonNull product_selection_ViewHolder holder, int position, #NonNull product_selection_Item model) {
holder.mProductname.setText(model.getmProductname());
holder.mFat_Value.setText(String.valueOf(model.getmFat_Value() + "% Fett"));
holder.mPrice.setText(String.valueOf(model.getmPrice().get(0) + " €"));
holder.isOffer = model.getisOffer();
if(holder.isOffer){
holder.mPrice.setTextColor(Color.parseColor("#D90000"));
}
else{
holder.mPrice.setTextColor(Color.parseColor("#000000"));
}
}
Don't know, if that's the best solution, but it worked for me! :)
Edit:
This only works like that, because I'm using the FirestoreUI with the FirestoreRecyclerAdapter.
Here, every getter with the Attribute of the Product_Selection_Item which has the same name inside the Firestore Storage, will be connected automatically to Firestore.
That's why I simply can do "holder.isOffer = model.getisOffer();"
I've followed this tutorial at YouTube.
I manage to search the data but now I want to open the details page of selected data on recycler view. How do I get it done? At which part should I implement the onClickListener for recyclerview? I've tried to put onClickListener, intent put extra ... in method OnBindViewHolder and SetDetails, but both of them have problems to recognize my context.
Here is my current code:
private void firebaseIngredientSearch(String searchIngredient) {
Toast.makeText(SearchActivity.this, "Started Search", Toast.LENGTH_LONG).show();
Query firebaseSearchQuery = databaseReference.orderByChild("ingName").startAt(searchIngredient).endAt(searchIngredient + "\uf8ff");
FirebaseRecyclerOptions<Ingredients> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Ingredients>()
.setQuery(firebaseSearchQuery, Ingredients.class)
.build();
FirebaseRecyclerAdapter<Ingredients, SearchActivity.IngredientsViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Ingredients, SearchActivity.IngredientsViewHolder>(firebaseRecyclerOptions) {
#Override
protected void onBindViewHolder(#NonNull IngredientsViewHolder holder, int position, #NonNull final Ingredients model) {
holder.setDetails(model.getIngName(), model.getIngStatus(),model.getIngCategory(),model.getIngCategory());
holder.view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent goDetail = new Intent(, IngredientSearchDetailsActivity.class);
goDetail.putExtra("ingName", model.getIngName());
goDetail.putExtra("ingStatus", model.getIngStatus());
goDetail.putExtra("ingCategory", model.getIngCategory());
goDetail.putExtra("ingDescription", model.getIngDescription());
mActivity.startActivity(goDetail);
}
});
}
#NonNull
#Override
public IngredientsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_searched_ingredient, parent, false);
return new IngredientsViewHolder(view);
}
};
firebaseRecyclerAdapter.startListening();
rvSearchResult.setAdapter(firebaseRecyclerAdapter);
}
////////////////////////////////////////////////////////////////////////////////
public static class IngredientsViewHolder extends RecyclerView.ViewHolder {
View view;
public IngredientsViewHolder(#androidx.annotation.NonNull View itemView) {
super(itemView);
view = itemView;
}
public void setDetails(String ingName, String ingStatus, String ingCategory, String ingDescription) {
TextView i_Name = view.findViewById(R.id.tvName);
TextView i_Status = view.findViewById(R.id.tvStatus);
TextView i_Category = view.findViewById(R.id.tvCategory);
TextView i_Description = view.findViewById(R.id.tvDescription);
ImageView iv_Status = view.findViewById(R.id.searchStatusIv);
i_Name.setText(ingName);
i_Status.setText(ingStatus);
i_Category.setText(ingCategory);
i_Description.setText(ingDescription);
}
}
User holder.view.setOnClickListener to initiate OnClickListener and use holder.view.getContext() instead of mActivity
holder.view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent goDetail = new Intent(, IngredientSearchDetailsActivity.class);
goDetail.putExtra("ingName", model.getIngName());
goDetail.putExtra("ingStatus", model.getIngStatus());
goDetail.putExtra("ingCategory", model.getIngCategory());
goDetail.putExtra("ingDescription", model.getIngDescription());
holder.view.getContext().startActivity(goDetail);
}
});
Im attempting a FireBase Horizontal RecyclerView (like an Netflix clone).
I have looked at Git and Android Arsenal to find programing similar, but had been unsuccessful finding anything with a firebase backend. I had played with the adapter and the database. I have work on this for a couple days without finding a solution.
public class RestaurantList extends AppCompatActivity {
RecyclerView recyclerView1;
RecyclerView recyclerView2;
FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference()
.child("Restaurants1")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference()
.child("Restaurants2")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter2.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
animationDrawable.setEnterFadeDuration(5);
animationDrawable.setExitFadeDuration(1000);
animationDrawable.start();
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);
recyclerView1.setLayoutManager(layoutManager);
recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
recyclerView1.setLayoutManager(layoutManager2);
loadAsianFood();
loadSteakHouse();
}
private void loadSteakHouse() {
adapter.startListening();
recyclerView1.setAdapter(adapter);
recyclerView1.getAdapter().notifyDataSetChanged();
recyclerView1.scheduleLayoutAnimation();
}
private void loadAsianFood() {
adapter2.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
adapter2.stopListening();;
}
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
adapter2.startListening();
}
}
It only runs the first category (Restaurant1) only.
A few problems I noticed, though I don't know if they'll solve your actual issue.
1 - You are calling startListening on the first adapter when it should be the second:
private void loadAsianFood() {
adapter.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
2 - You are only stopping the first adapter, not the second.
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
3 - You are only resuming the first adapter, not the second:
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
}
RecyclerView recyclerView1;
RecyclerView recyclerView2;
FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference("RestaurantCategory")
.child("01").child("SteakHouse")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference("RestaurantCategory")
.child("02").child("AsianFood")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter2.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
animationDrawable.setEnterFadeDuration(5);
animationDrawable.setExitFadeDuration(1000);
animationDrawable.start();
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);
recyclerView1.setLayoutManager(layoutManager);
recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
recyclerView1.setLayoutManager(layoutManager2);
loadAsianFood();
loadSteakHouse();
}
private void loadSteakHouse() {
adapter.startListening();
recyclerView1.setAdapter(adapter);
recyclerView1.getAdapter().notifyDataSetChanged();
recyclerView1.scheduleLayoutAnimation();
}
private void loadAsianFood() {
adapter2.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
adapter2.stopListening();;
}
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
adapter2.startListening();
}
}
I want to read the value of a child(name) from Client's nodes when I use a firebase recycler adapter from Request's nodes which have childs with the value of Client's keys
I tried this, but I only received the link
adapter=new FirebaseRecyclerAdapter<Request, OrderViewHolder>(optionsReq) {
#Override
protected void onBindViewHolder(#NonNull OrderViewHolder holder, final int position, #NonNull final Request model) {
holder.txtOrderId.setText(adapter.getRef(position).getKey());
holder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
holder.txtOrderAddress.setText(model.getAddress());
holder.txtOrderDate.setText(Common.getDate(Long.parseLong(model.getDate())));
holder.txtOrderClient.setText(client.child(model.getDniclient()).child("name").toString());
#NonNull
#Override
public OrderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout, parent,false);
return new OrderViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
DatabaseReference databaseReference;
String name;
adapter=new FirebaseRecyclerAdapter<Request, OrderViewHolder>(optionsReq) {
#Override
protected void onBindViewHolder(#NonNull OrderViewHolder holder, final int position, #NonNull final Request model) {
holder.txtOrderId.setText(adapter.getRef(position).getKey());
holder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
holder.txtOrderAddress.setText(model.getAddress());
holder.txtOrderDate.setText(Common.getDate(Long.parseLong(model.getDate())));
holder.txtOrderClient.setText(client.child(model.getDniclient()).child("name").toString());
databaseReference = FirebaseDatabase.getInstance().getReference().child("Client").child(model.getDniclient());
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
name = dataSnapshot.child("name").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
#NonNull
#Override
public OrderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout, parent,false);
return new OrderViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
I have implemented RecyclerView with Firebase RecyclerAdapter in my activity and want to get onClickListener implemented to it although I have tried to achieve this but something is not right in my code I guess, please help me find what is wrong.I have searched another questions but did not got the understanding and cant find the solution to my problem.
I am using Android Studio
firebase Adapter class file:
public class HomePage extends AppCompatActivity {
#Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sentToStart();
}
FirebaseRecyclerAdapter<post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
#Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setwebViewPost(model.getWebViewPost());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder {
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mClickListener.onItemClick(v, getAdapterPosition());
}
});
}
private postViewHolder.ClickListener mClickListener;
public interface ClickListener {
public void onItemClick(View view, int position);
}
public void setOnClickListener(postViewHolder.ClickListener clickListener) {
mClickListener = clickListener;
}
public void setwebViewPost(String webViewPost) {
WebView webView = (WebView) mView.findViewById(R.id.webView_news);
webView.loadUrl(webViewPost);
}
}
#Override
public postViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
postViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnClickListener(new postViewHolder().ClickListener() {
#Override
public void onItemClick (View view,int position){
Toast.makeText(this, "Item clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return viewHolder;
}
Move the onClickListener inside onBindViewHolder.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new postViewHolder().ClickListener() {
#Override
public void onItemClick (View view,int position){
Toast.makeText(this, "Item clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
}
EDIT:
Remove the interface because it's redundant.Your are passing the value from viewholder to onCreateViewHolder which is unnecessary. Instead handle the click inside viewholder. Check the updated class.
public class HomePage extends AppCompatActivity {
#Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sentToStart();
}
FirebaseRecyclerAdapter<post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(post.class, R.layout.post_row_recycle_home, postViewHolder.class, mDatabaseReference) {
#Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setwebViewPost(model.getWebViewPost());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder {
View mView;
public postViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Show the webViewPost Here
}
});
}
public void setwebViewPost(String webViewPost) {
WebView webView = (WebView) mView.findViewById(R.id.webView_news);
webView.loadUrl(webViewPost);
}
}
#Override
public postViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
postViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
return viewHolder;
}
}
Please see my approach which was taken from Google samples + GoogleIO youtube sessions:
Use:
onCreateViewHolder(ViewGroup parent, int viewType)
newsAdapter = new FirebaseRecyclerAdapter<ModelNews, NewsItemHolder>(
ModelNews.class,
R.layout.item_news,
NewsItemHolder.class,
FirebaseDatabase.getInstance()
.getReference(FIREBASE_NEWS)
.orderByChild(FIREBASE_ORDER_BY_FIELD)
.limitToFirst(20)) {
#Override
public NewsItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
final NewsItemHolder newsItemHolder = new NewsItemHolder(view);
newsItemHolder.holder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = newsItemHolder.getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
ModelNews clicked = newsAdapter.getItem(pos);
if (clicked.getIsExternal() && !hasConnection) {
showSnackbar(R.string.news_with_wifi);
return;
}
goToDetails(clicked.getID(), clicked.getIsExternal());
}
}
});
return newsItemHolder;
}
#Override
public void populateViewHolder(
final NewsItemHolder view,
ModelNews data,
final int position) {
// Set news title
view.titleView.setText(data.getTitle());
// hide OR show wifi icon
view.needWifiIcon(data.getIsExternal());
// Set news image
mImageLoader.loadImage(data.getThumbUrl(), view.thumbView, true);
// Set news description
view.descView.setText(data.getContent());
}
#Override
public void onDataChanged() {
if (newsProgessBar.getVisibility() != View.GONE) {
newsProgessBar.setVisibility(GONE);
}
}
};