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.
Related
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());
}
});
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
}
}
I have a Custom BaseAdapter showing a ListView containing a TextView and a CheckBox. The TextView has an OnClickListener implemented to perform a specific action when clicked on the text. Within the Adapter I have a OnCheckedChangeListener registered which keeps track of which item is checked (because of the ListView recycling).
I want to start an ActionMode when I check a CheckBox and stop it when I uncheck it.
How can I inform the main activity hosting the adapter that a check has been made?
Create a listener call back interface, something like:
public interface ListenerCheckBox
{
public void onRowChecked(int rowNun);
}
Then, make your main activity implement this listener:
public class ActivityMain implements ListenerCheckBox
Then, when you instantiate your custom BaseAdapter, pass in the listener:
//kv 3rd parameter would be listener
CustomBaseAdapter customBaseAdapter = new CustomBaseAdapter(this, items, this);
Then, in the constructor of your CustomBaseAdapter, set a member field to the listener:
public CustomBaseAdapter(Context context, ArrayList<Item> items, ListenerCheckBox listenerCheckBox)
{
mListenerCheckBox = listenerCheckBox;
...
}
Then, every time an item is checked, call:
mListenerCheckBox.onRowChecked(rowNum);
Here is a Android Studio project that shows an example of how to do what you are asking. In particular, check out the check listener and its onClick():
#Override
public void onClick(View v) {
if (mMode != null) {
mMode = mListView.startActionMode(new ExampleMultiChoiceModeListener(mActivity));
}
mListView.setItemChecked(mPosition, ((Checkable) v).isChecked());
}
I am using a custom list view by extending the ArrayAdapter class. Each row has some text as well as an image. What I want to do is delete the row when I click on the imagevView. So i set an onclick listener as well as a tag for each image view and then used the onclick listener to change the adapter. However it simply refuses to work. What exactly am I doing wrong?
holder.image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
Toast.makeText(context, "ImageView clicked for the row = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
createEvent.list.remove(view.getTag());
createEvent.adapter.notifyDataSetChanged();
}
The toast prints correctly. However the item is not deleted. Any ideas on how I can achieve this?
In my Android application I have a custom ArrayAdapter for managing rows in a List that implements onCLickListener.
In the onClick() method I want to show the context menu.
public class MyAdapter extends ArrayAdapter<MyClass> implements
OnClickListener {
public void onClick(View v) {
v.showContextMenu();
}
}
In my main List Activity class I override the onLongListItemClick() method that starts a different Activity.
When I click on a row in the list - the context menu is shown correctly but a second later the onLongListItemClick() is also executed. How to stop this happening?
thanks!
After your comments, I think I have a solution. Don't use the row onClick for the context menu, set a click listener in your adapter for the checkbox and show your context menu from there. That way you avoid the onLongListItemClick.
To make both the checkbox and the row clickable, add this to the checkbox definition in your XML:
android:focusable="false"
android:focusableInTouchMode="false"
EDIT
This should do the trick:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
View v = buttonView.getParent(); // get the parent row view containing the checkbox
v.showContextMenu(); // show the context menu
}
}
}