let's say you have a listview with custom xml-objects on it in a row.
But you want to address specifically the textview, if it's press in the OnItemClickListener. What's the way - or bette - the best practise to do so? If i check in the OnItemClick method, the specifically element in the row (e.g. the textview, doesn't get recognized.
You can add onClickListener to that textView in the getView method of your adapter like this.
viewHolder.myTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"item= "+data.get(position).getTitle());
}
});
For this to work create a class which extends BaseAdapter class and implements all its method
1.Now in the getView() method of this class initialize all the required views in a single item of listview
2.After that attach onClickListener() to the desired textView.
3.After that select your custom adapter class in the listView.setAdapter() method
This should help you if it doesn't work use RecyclerView instead and do all the above task in the onCreateViewHolder() method of RecyclerView.adapter Class
if you have model class name **User** now you can get user info by using
viewHolder.tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
User user = data.get(pos);
user.getTitle();
Log.d(TAG,"item= "+data.get(position).getTitle());
}
});
Related
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
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 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:---
This is a my use case. I have an activity with a listview and a textview. The textview is the sum of all numbers on the listview, the listview has all the numbers.I have a custom adapter for this case.
On each row of the listview, I have a button. This button will change the number on this row.
what I want to do is this:
when user clicks the button, the value on each row is changed - doable.
the sum on the textview also changes accordingly.
This is a simplified example. In reality, I also have a constraint that I have a data model to represent the data on each role. I am not able to extend the data model to DataSetObserver.
any help?
You should create a listener, which listens for touch on your increment button and inside it call a method of the class extending an interface you created.
To make it easy, in your adapter you will have:
public interface OnIncrementListener{
onNumberIncremented();
}
private OnIncrementListener mListener;
//This is inside getView method of your adapter
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.onNumberIncremented();
}
});
Then your activity
public class MyActivity extends Activity implements OnIncrementListener {
//inside onCreate
myAdapter.setOnIncrementListener(this);
//end onCreate
#Override
public void onNumberIncremented() {
//Change value of your TextView here
}
}
In my android project I am Using a custom listview to show information. In each element of the listview(Each row in list view) have several clickable forcussable textviews. And I added lstvw.setItemsCanFocus(true), for listview and now i can select any clickable textview and click.
----------------
textviewclick1
textviewclick2
----------------
textviewclick1
textviewclick2
----------------
continue......
But can't figure out how exactly handle the onClick event for each clickable textview as getView() method in the BaseAdapter class is bit confusing for me.
I implemented onclickListener in the ListviewAdapter class that I extended BaseAdapter and created.
public class ListVWAdapter extends BaseAdapter implements OnClickListener{ implementation...}
can anyone suggest me how to handle onClick(View view) method to handle all the clickable textview items.
I tried following way but did't work correct. I globally declared the ViewHolder viewholder since I want to access it in this method rather than declaring it in normal way within the getView() method. But initializing normal way by checking if (convertView == null)
#Override
public void onClick(View v) {
if(v==holder.txtViewTitle)
Toast.makeText(v.getContext(), "Link1 : "+ String.valueOf(currentPosition), Toast.LENGTH_LONG).show();
if(v==holder.txtViewDescription)
Toast.makeText(v.getContext(), "Link2 : "+ String.valueOf(currentPosition), Toast.LENGTH_LONG).show();
}
In list item xml, for <TextView..> set android:onClick="onFirstLinkClick". Similarly for second TextView.
Then in activity class implement this
public void onFirstLinkClick(View V) {
// handle click
}
Similarly another method for second textview. That should work.