"Cannot resolve symbol ItemClickListener" - java

May i know why is it showing me the error of "Cannot resolve symbol ItemClickListener"? Do i need to add library or something in order to solve this issue or i shouldn't declare it in here?
public static class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
View view;
public FoodViewHolder (View v){
super(v);
view= v;
}
public void setName(String title){
TextView post_title = (TextView)view.findViewById(R.id.Menu_Name);
post_title.setText(title);
}
public void setImage(Context ctx, String image){
ImageView menuImage = (ImageView) view.findViewById(R.id.Menu_Image);
Picasso.with(ctx).load(image).into(menuImage);
}
#Override
public void onClick(View v) {
}
}

In General for RecyclerView we create an Interface for handling the click events. Unlike a normal Button Click, the RecyclerView Click events cannot be handled directly. As the RecyclerView is the Adapter(Data Provider to the View), You cannot directly handle the item clicks from here and update the view. For this you need an Separate Interface which is ItemClickListener in your case (Create an Interface file separately in your project). In that Interface you need to declare a method for example something like this
public Interface ItemClickListener{
void onRecyclerViewItemClicked(int position);
}
Create a OnClickListener for your View(which is present over single row. Eg: image, text, etc);#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemClickListener.onRecyclerViewItemClicked(position);
//itemClickListener is the Interface Reference Variable
}
});
}
And in your Activity you need to implement this Interface like
public class YourActivity extends AppCompatActivity implements ItemClickListener {
....
....
protected void onCreate(Bundle savedInstanceState) {
...
...
}
#Override
public void onRecyclerViewItemClicked(int position) {
//You will get the position of the Item Clicked over recycler view
//You can handle as per your requirement
}
}
Upon doing this you will listen the click event of recyclerview to the activity. Then you can handle it accordingly.
If you have further doubt follow the references:
https://stackoverflow.com/a/40584425/8331006
https://stackoverflow.com/a/28304164/8331006

you can set the click listener to any view you want you don't need to declare it as a seperate
yourView.setOnClickListener(this);
and in your onClick(View v) add the code that you want whenever you click the view you want for example :
if(v==imageView){
//write your code here
}

Related

set progress bar value from other class android

I have a progress bar in viewHolder class inside adapter class. I need to update that bar from another activity UpdateActivity when the method onPause that activity is implemented. I have tried using the interface but it does not update the progress bar, the bar remains as before with no error as well. Is there any way I can update the bar from given activity? Any help would be appreciated.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
public static class MyViewHolder extends RecyclerView.ViewHolder
implements MyCallBack {
ProgressBar progressBar;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
progressBar = itemView.findViewById(R.id.progress_bar);
}
#Override
public void updateMyText(int integer) {
progressBar.setProgress(integer);
}
}
}
interface MyCallBack {
public void updateMyText(int integer);
}
}
public class UpdateActivity extends AppCompatActivity {
MyCallBack myCallBack;
int final_value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
#Override
protected void onPause() {
super.onPause();
/* myCallBack = new MyCallBack() {
#Override
public void updateMyText(int integer) {
progressBar.setProgress(100);
}
}; */
final_value = position.getValue(); // final_value != null
progressBar.setProgress(final_value);
}
}
Where is your recyclerview for which you implemented the adapter?
I do not have a clear understanding of the task, what do you want to do here in the activity, it looks like it is a progress bar for loading a splash screen or something similar, then why do you need a recycler for this?
If the logic is that each item in the recyclerview (or another view) contains a progress bar, then in order to manage each specific progress bar you need:
Public method for set progress value in the MyViewHolder.
Reference to the concrete which view contains progressbar (for example, you can use some listener for this inside adapter constructor).
For interaction between activities you can use method startActivityForResult.

Change BottomNavigationView with a button inside a RecyclerView Item

The application has a BottomNavigationView for navigating between fragments. One of the Fragments related to the BottomNavigationView has a Fragment as a child containing a RecyclerView and each RecyclerView Item has a Button attached to it.
I would need to navigate the BottomNavigationView to another Fragment with an OnClick of the Button (Highlighted with the red) inside the RecyclerView Item. I have tried different ways but I have not gotten it to work so far.
The Click is handled inside the Adapter of the RecyclerView. (The code inside the OnClickListener is just to clarify what I am trying to do.)
#Override
public void onBindViewHolder(#NonNull LocationsViewHolder holder, int position) {
...
holder.showMarkerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomNavigationView navigation = (BottomNavigationView)v.findViewById(R.id.nav_view);
navigation.setSelectedItemId(R.id.navigation_map);
}
});
}
The easiest way is created an interface implemented by your Activity
First create an interface that i called OnItemClick :
public interface OnItemClick{
void onItemClick();
}
Following on your activity implement this interface like below :
public class MainActivity extends AppCompatActivity implements OnItemClick{
/*
rest of your code
*/
#Override
public void onItemClick() {
navigation.setSelectedItemId(R.id.navigation_map);
}
}
On your Fragment you need to pass the Activity into your Adapter
YourAdapter adapter = new YourAdapter(requireActivity());
And on your adapter you need to initialize the interface like below :
OnItemClick listener;
public YourAdapter(Activity activity) {
listener= ((MainActivity) activity);
}
And finaly to call the method on your activity just call it like below
#Override
public void onBindViewHolder(#NonNull LocationsViewHolder holder, int position) {
...
holder.showMarkerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick();
}
});
}

Why won't my new activity start on startActivity(intent)? [duplicate]

I'm trying to implement a way to handle item selection on a RecyclerView. I personally don't like the way suggested in some answers on SO of passing through gestures, and I thought that implementing an OnClickListener, as suggested here and here, was waaay cleaner.
The fact is that... this pattern doesn't actually work! I'm really not able to understand why my OnClickListener.onClick is never called. It's kinda like another method intercepts the click before onClick can take care of it.
This is my code:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvName;
ImageView star;
public ViewHolder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.CHAT_ITEM_name);
star = (ImageView) itemView.findViewById(R.id.CHAT_ITEM_star);
Fonts.setTypeface(tvName, regular);
}
#Override
public void onClick(View view) {
int position = getLayoutPosition();
select(position);
}
}
Unfortunately it's very important for me to able to access the position of the clicked item in the whole dataset, in order to remove it, so doing something like indexOfChild isn't acceptable too: I tried, but this method gives you the position of the item in the visibile part of the list, thus making list.remove(position) impossible.
Looking at the updated code: you are not setting the onClickListener to any of the views in the ViewHolder. It is an understandable mistake to forget the click listener.
Just use:
tvName.setOnClickListener(this);
star.setOnClickListener(this);
You can set to both or just one of them. You can also simply get the parent layout of these two views, so that the whole item itself in the adapter can be clickable.
itemView.setOnClickListener(this);
You can do it in your onBindViewHolder
#Override
public void onBindViewHolder(ReportViewHolder holder, int position {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// handle your click here.
} });
}
Simplely Click Handler your ViewHolder. Recycler View don't have special attaching click handlers like ListView which has the method setOnItemClickListener().
** public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
** in public ViewHolder(Context context, View itemView) set public void onClick(View view)
** get position by: int position = getLayoutPosition(); User user = users.get(position);

How to set OnClick listener on Recycler View Adapters from within Acivity class? Or Access other RVAdapter from first Adapter?

I have a layout where I use two RecyclerViews, each with its own Adapter.
On the Activity class of the layout I have the references of the RVs and I create them there. Then I dynamically create the buttons and insert them on one of the RecyclerViews.
The current problem I'm facing is that I'm setting the onClick listener inside the adapter itself but I want to hopefully set it through the activity class.
That's because the activity class has references to all the things I need to know when an object inside the recycler view is clicked and I want them to move between the RVs when clicked. From first RV to go to second and vice versa.
To create the view inside the first adapter I use.
for (Iterator<String> it = setList.iterator();it.hasNext();)
{
String str = it.next();
Object1 o = new Object1 (str, R.drawable.testicon);
objectList.add(o);
}
firstRVAdapter.notifyDataSetChanged();
Inside my adapter I set the onclick listener inside the OnBindViewHolder as such:
#Override
public void onBindViewHolder(LeftViewHolder holder,final int position) {
holder.imageView.setImageResource(verticalLeftList.get(position).getLogo());
holder.textView.setText(verticalLeftList.get(position).getName());
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String productName =
verticalLeftList.get(position).getName().toString();
Toast.makeText(context, productName + " is selected",
Toast.LENGTH_SHORT).show();
}
});
}
Any recommendations or help how to change this to allow me to do what I want would be really appreciated. Either searching for a way to access the second adapter through the first one or a way to set the listener for each item inside RV from the activity class.
You can create a callback for your adapter to pass data back to your activity.
public interface AdapterCallback {
void itemClicked(int position);
}
see Juan's answer for example
Pass the Activity to the adapter in the constructor as a callback listener, and then, in the onClick() method call a callback on the activity.
(...) means there is more code not relevant to the answer.
In the RV Adapter class:
public MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
Listener listener;
public MyAdapter(Listener listener, ...){
this.listener = listener;
...
}
#Override
public void onBindViewHolder(LeftViewHolder holder,final int position) {
holder.imageView.setImageResource(verticalLeftList.get(position).getLogo());
holder.textView.setText(verticalLeftList.get(position).getName());
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String productName =
verticalLeftList.get(position).getName().toString();
Toast.makeText(context, productName + " is selected",
Toast.LENGTH_SHORT).show();
listener.onClick(productName);
}
});
}
...
public static interface Listener{
public void onRVClick(String aParamToIdWhatWasClicked);
}
}
And make the Activity implement the Listener interface and pass its instance to the RV Adapter constructor.
public class MyActivity extends AppCompatActivity implements MyAdapter.Listener {
private MyAdapter myRvAdapter;
#Override
public void onCreate(Bundle savedInstanceState){
...
myRvAdapter = new MyAapter(this, ...);
...
}
...
#Override
public void onRVClick(String what){
System.out.println(what + " was selected");
}
...
}
Note that the parameter of the onRVClick() interface method can be whatever you want. It could even be the data list item backing the selected recycler view item.

How can I get the text from EditText of a recyclerview item when user clicks on a button of the same item?

I am designing a RecyclerView list. Each item of the Recyclerview contains a LinearLayout. This LinearLayout contains two views, the first one is an EditText and the second one is Button. When user taps on the button, it fires an onclick event. From onClick listener, I need to get the content of the EditText. I don't find a way to access the content of a sibling view when the user taps on another sibling view.
My question is not "how can I set on click listener to a button inside adapter". Most of the people answered how to set onClick listener to a button which is there inside the recyclerview item. My question is bit different, when I am inside onClick method which is fired from button, how will I access the edittext which is a sibling of button. Every item has one edittext, so when I click on a button how will I find the correct edittext?
For example, I have a recylerview of size 10. And each item of recyclerview contains a LinearLayout and inside linearlayout two item, one is an Edittext and the other one is a Button. when I tap on 7th items button, how will I get the text of 7th item's Edittext? I hope I have explained it well
Any help would be appreciated.
First of, you need two references: one to your EditText and one to your Button. You can get those in your ViewHolder. Next, you need an OnClickListener. The ViewHolder can conveniently also implement one but you could also use onBindViewHolder() for that.
Inside that OnClickListener you can filter out your id with a switch statement if you want to and then get the content of the EditText like this:
switch(viewId) {
case R.id.buttonId:
String text = editText.getText().toString();
// do something with that text
return true;
}
In case you implemented an OnClickListener in your ViewHolder you can then do this button.setOnClickListener(this); inside your ViewHolder to make sure onClick() is actually called when you click the button.
EDIT:
Here's some sample code that should work for your case. I'm implementing View.OnClickListener here as mentioned above.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
EditText editText;
Button button;
public ViewHolder(View itemView) {
super(itemView);
editText = itemView.findViewById(R.id.editText);
button = itemView.findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.button:
String text = editText.getText().toString();
break;
}
}
}
This is what it would look like if you were to do it in your onBindViewHolder() (in this case you would NOT implement the OnClickListener in your ViewHolder obviously):
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = holder.editText.getText().toString();
}
});
}
Step 1 :Make an abstract function in your adapter.
abstract void onButtonClicked(String text);
Step 2: Declare your adapter Abstract.
Step 3: Override the method (onButtonClicked(String text);) in your activity were you have instantiated the adapter.
Step 4: In your adapter inside the onClickListener for your button call the function :
onButtonClicked(editText.getText().toString());
and you'll get the string in your activity where you overrided the method.
You can use holder pattern for RecylcerView adapter. Then you can set click listener on your button and get the text from EditText.
public class SimpleViewHolder extends RecyclerView.ViewHolder {
private EditText simpleEditText;
private Button simpleButton;
public SimpleViewHolder(final View itemView) {
super(itemView);
simpleEditText = (EditText) itemView.findViewById(R.id.simple_edit_text);
simpleButton = (Button) itemView.findViewById(R.id.simple_button);
simpleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = simpleEditText.getText().toString()
}
});
}
}
You can do it inside your adapter which is being set on the recycler view.
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
}
}
And in the bindview holder you can access the views
public void onBindViewHolder(#NonNull final ImageAdapter.MyViewHolder holder, int position) {
holder.mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your Code to access the edit text content
}
});
}
use a custom listener like this in the holder:
public class SimpleViewHolder extends RecyclerView.ViewHolder {
private EditText simpleEditText;
private Button simpleButton;
public SimpleViewHolder(final View itemView, final OnItemSelectedListener listener) {
super(itemView);
simpleEditText = (EditText) itemView.findViewById(R.id.simple_edit_text);
simpleButton = (Button) itemView.findViewById(R.id.simple_button);
simpleButton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String text = simpleEditText.getText().toString();
if(listener != null) listener.onItemSelected(text);
}
});
}
public interface OnItemSelectedListener{
void onItemSelected(String value);
}
}
Simply Use Your ViewHolder. It contains all the children you want. Implement the code inside your adapter where each item is inflated. Here is an example.
//inside the onBindViewHolder
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = viewHolder.editText.getText().toString();
Log.d("output", text);
}
});
If you are using FirebaseUI Implement this inside the populateViewHolder for older version and onBindViewHolder for the later versions.
People put -1 when they don't know the answer or when they do not understand question. So funny!! In onclick I took parent(getParent()) from the view and accessed the second child of the parent. With that I am able to access the content of the sibling.
` public void onClick(View v) {
for(int i = 0;i<parent.getChildCount();i++){
if(parent.getChildAt(i)instanceof EditText){
passwordView = (EditText)parent.getChildAt(i);
}
}
}`

Categories