How to get values of EditText from another model / Make an o - android

I'm trying to make an on-click method for recycle List items.
I have found that you can make it inside the Adapter you have made for the list inside the OnBindViewHolder, and it works, however, I do not have access to the EditTexts that are in the Activity where the Adapter is used , I was wondering if there is a way to import them to be used in the Adapter ? Thank you in advance.

You can do it, You have to use Interface for this purpose.
Interface will pass your data from Adapter to your Activity having that EditText.
Then you update your EditText accordingly.
Create an Interface
public interface DataTransferInterface {
public void onItemClicked(String data);
}
Create Object of Interface in Adapter
DataTransferInterface dtInterface;
Initialize it in Constructor of your adapter
public MyAdapter(DataTransferInterface dtInterface) {
this.dtInterface = dtInterface;
}
When Any item is Clicked in Adapter pass data to Activity
item.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dtInterface.onItemClicked("Data to be passed here");
}
});
Now in your Activity
implement that interface like:
public class MainActivity extends Activity implements DataTransferInterface {
When you implement Interface in Activity, It will show error to implement its required methods. SO,
#Override
public void onItemClicked(String Data) {
// TODO Update your EditText Here
}
Initialize your adapter like
myAdapter = new MyAdapter(this);
If you find any difficulties, You can read more here

Related

How to call clicklistener of an item's textview inside a recyclerview outside the adapter?

I have recyclerview inside an activity. I have some View inside each item. Suppose a textview, I am implementing a click listener on that particular view.
However, there is a case where i have to call the clicklistener of a particular position of that textview.
Since there is no function to call the onclick where i can pass the position.
I thought holder.itemview.callonclick() would work. But it is not working.
is there any way to call the itemview's textview of a particular position of a recyclerview.
You can create custom click listener using interface like below.
public interface ClickInterface {
public void recyclerviewOnItemClick(int position);
}
make sure you implement this interface to class where you want your code to be executed and implement it's method.Then you can pass this listerer's refrence to adapter like this.
TestAdapter adapetr = new TestAdapter (this);
listview.setAdapter(adapter);
In adapter :
private ClickInterface clickInterface;
public TestAdapter (ClickInterface clickInterface) {
this.clickInterface = clickInterface;
}
viewHolder.txtview.setOnClickListener(v ->
clickInterface.recyclerviewOnItemClick(position));
-You will get implemented method call when textview is being clicked.
You can try like this in Holder class,
mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getAdapterPosition()== YOUR_POS)
{
code..
}
}
});
or in onBindViewHolder ,`
if(i==YOUR_POS){ holder.mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
code..
}
});}

How to access RecyclerView viewholder inside main activity in android?

I have a list in activity with dynamic data.When i click on a button inside list item,I want to hide any text from list item.From adapter class it can be possible to get views from custom layout but from activity how to do this?
Just change the data of that particular position and then call YourCustomUpdateFunc:
//Function inside your RecyclerAdapter class
public void YourCustomUpdateFunc(int pos){
notifyItemChanged(pos);
}
Then, the onBindViewHolder function will do its work.
First Create local Variable in adapter class as
OnCustClick mclickListner;
then in viewholder override method like
#Override
public void onClick(View v) {
if (mclickListner != null) {
mclickListner.onItemClick(v, getAdapterPosition());
}
}
after that add this methods in your adapter class
public interface OnCustClick{
void onItemClick(View view, int position);
}
public void setOnCustClick(OnCustClick mclickListner)
{
this.mclickListner = mclickListner;
}
and in activity implement interface

I have 3 buttons inside recyclerview whose click event is to be set on main activity and not in adapter class?

How can I make onclick listener of recyclerview button in main activity and perform click from main activity.
create a interface in your adapter class and implement it in your activity class.
public interface handleClick
{
public void onFirstButtonClick();
public void onSecondButtonClick();
public void onThirdButtonClick();
}
and when creating your adapter in your activity class pass a additional parameter YourAdapter(yourparamters,this)
and now in your adapter
when you intialize your values from construcor assign this to your inteface
Adapter Class
Youradapter(yourParamters,handleClick)
{
this.handleClick=handleClick;
}
now in your onBindViewHolder create onClickListener for your buttons and on
onClick call interface methods like this
button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
handleClick.onFirstButtonClick();
}
});)
so the method onFirstButtonClick which you will have to implement in your activity class will be called.
You have to do following thing
Firstly create an interface for listening on click event from adapter to activity.
Second implement that interface in your activity.
Third create the instance of your interface in your activity and pass it to adapter.
Four in adapter call your method of interface using instance received at adapter and do your work in your activity.If you will upload code then i will correct.

How to Get a callback for activity started from inside arrayadapter for listview in Android?

How to start an activity from inside ArrayAdapter and get a call back when the activity is finished(just like onActivityResult)?
Following code is on post execute of an asynctask that is started on the button click of a button displayed in each listview row.
((Activity) mContext).startActivityForResult(intent, AppConstants.DUMMY_CONSTANT);
If it's possible, try using this instead:
listView.setOnItemClickListener(new OnItemClickListener() { ... });
Edit. You can also create interface, implement it in your acitvity and pass it to your adapter
My Pscudoe code:-- Create call back method using interface
and make call back wherever you want
MyListener listener;
public interface MyListener {
// TODO: Update argument type and name
void onClick(View view, int position);
}
#Override
public void onClick(View view) {
listener.onCardClick(view, getPosition());
}
From Listview onitem:---

Android create interface listener for onClick in Adapter

in my application i'm create simple interface to detect after click on adapter items into parent activity.after create interface and method into adapter i can not how to use this interface to call listener, for example:
Interface Class:
public interface IOnClickListListener {
public void onClick(boolean click);
}
summurized Adapter:
private static class ViewHolder {
public ViewHolder(View view) {
}
public void fill(final ArrayAdapter<SubjectListStructure> adapter, final SubjectListStructure item, final int position) {
root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* call interface to listen that */
}
});
}
}
public void setOnClickListener(IOnClickOnNiyazmandihaListListener l){
iOnClickOnNiyazmandihaListListener = l;
}
in this adapter after click on item, my interface must be call. now i want to listen that into activity by this code:
subjects_adapter.setOnClickListener(new IOnClickOnNiyazmandihaListListener() {
#Override
public void onClick(boolean click) {
Toast.makeText(G.currentActivity,"ddddd", Toast.LENGTH_SHORT).show();
}
});
now how to call listener into adapter ?
if you want to trigger/invoke the callback, simply call
boolean click = true
iOnClickOnNiyazmandihaListListener.onLogin(click)
from the adapter class
In my honest opinion, if you intend to register the click event inside an adapter, you must pass the position of item which was clicked, instead of passing a boolean value to check if item was clicked or not. In this way, you will get a number of options to handle the clicked item callback.
Here is a simple implementation from my side according to your use-case.
Your interface class just require a small change as follows:
public interface IOnClickListListener {
public void onClick(int positionOfItem);
}
Inside your adapter class, at the onClick() of your root's setOnClickListener, you must call the interface's method in the following way, considering iOnClickOnNiyazmandihaListListener is a member variable of your adapter class
iOnClickOnNiyazmandihaListListener.onClick(position)
where position is passed as a parameter to setOnClickListener and it is the actual position of item clicked.
In this way, wherever you want this onClick() method to be called, you have to provide implementation of the interface in that class, about what you want to do with the clicked position when onClick() is triggered.
So whenever any list item is clicked, your custom implementation of the onClick() method will be used, and the code which you put inside that implemented method will be called.
Example:
Suppose you implemented this interface in an activity, then it tell you to override the onClick() method of your interface inside that activity, i.e. to provide onClick() implementation
So implementation of onClick() would look similar as follows:
#Override
void onClick(int position){
// do what you want to do with clicked item's position
Toast.makeText(this, "Item at position:" + position + " clicked", Toast.LENGTH_LONG).show();
}
For that, your class must have implements IOnClickOnNiyazmandihaListListener as its starting line.

Categories

Resources