I have an Android ListView with a bunch of rows. Each row contains an image and some text. If I click on the image of a row, I want to perform action A, if I click on any other place in the row, I want to perform action B. Both of these actions need the id of the ListView item to work.
So far, for action B, I simply used listview's onItemClick, where I got the id from the parameter. However, as far as I know, the only way to find out on which view in the row itself the user clicked, is to add an onClick event handler to the ImageView that triggers action A. In that case, I only get the View that was clicked, though - not the id of the row that the ImageView is in.
My question is - how do I go from a View object to getting the id of the parent row within the ListView?
The onItemClickListener method has an onClick interface already with the view and position passed into it: You can get check which view has been clicked by doing a switch statement and performing a dedicated action when the specific view has been clicked.
E.g.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
}
});
as shown in How to create listview onItemclicklistener
You'll see that in the onItemClick interface it has the View view as a parameter and int position as a parameter, perform the action inside the listener using both of those parameters. Like i mentioned before you can use a switch statement.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
switch(view.getId()) {
case R.id.imageView:
//perform action for imageView using the data from Item (if you have used an MVC pattern)
break;
// you can do this for any widgets you wish to listen for click actions
}
}
});
Update: All credit for this goes to wwfloyd in How to know which view inside a specific ListView item that was clicked
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line, parent, false);
}
// This chunk added to get textview click to register in Fragment's onItemClick()
// Had to make position and parent 'final' in method definition
convertView.findViewById(R.id.someName).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
// do stuff...
}
in your adapter apply a click listener to the element and then trigger the interface method, so that you can identify the registered widget that has been clicked and then in your onItemClick do:
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
long viewId = view.getId();
switch (viewId) {
case R.id.someName:
//widget has been clicked
break;
default:
//row has been clicked
break;
}
}
if you aren't using tag, then you can
int itemId=...;
OnClickListener customOnClick=...;
imageView.setTag(itemId);
imageView.setOnClickListener(customOnClick);
and inside your custom OnClickListener
int itemId = (Integer) view.getTag();
note that you can set "any" Object as tag, so you may even set your whole custom object, not only id
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id)
in this method View arg1 gives you Item view and you can find the item content by this arg1.findViewById(R.id.content);. Here position gives you the item position of list view
You can set click listeners for each view in a row from the getView() method in the adapter.
You can impliment it in getView() method of adapter of listview.
I have a custom ArrayAdapter to show items in a ListView. It has a Button and a TextView, I want that when the button gets pressed, change the text view's color to red, but if I click the button of another item of the list, besides of setting the textview of the item to red, it will set the textview's color of the rest of the items, to their original color.
It would be different if I were using android.R.layout.simple_list_item_1 as the layout of the ListView's childs. I would only have to override the setOnItemClickListener of my list like this:
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv1 = (TextView)view;
if(tv1.getCurrentTextColor() != Color.RED){
for(int i = 0; i < myList.getChildCount(); i++){
TextView tv2 = (TextView)myList.getChildAt(i);
tv2.setTextColor(tv1.getCurrentTextColor());
}
tv1.setTextColor(Color.RED);
}
}
});
But since I'm using a custom layout(also a custom adapter), I do not know how and where should I manage this, in the OnItemClick() method or in the getView() method of my custom adapter.
Let's say you have a dialogfragment with a listView. The title of the dialog is a custom view which is inflated in onCreateDialog(). Is it possible to toggle visibility of the inflated view when the list item is clicked.
In the onCreateDialog() function, you can hold the dialog view which referencing to title view which you want to toggle on.
On click event of listview.
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//Toggle your title here
}
});
Well I have a listview with an OnItemClickListener on it, each item of listview is a customview with two textviews, I want to get text of first textview when an item is clicked, So how can I do that? when an item is clicked I can just get the view that is clicked but how can I get textview on this customview?
Traverse that view's hierarchy:
TextView textView = (TextView) clickedView.findViewById(R.id.first_text_view);
clickedView is provided to you by OnItemClickListener.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textFromFirstView = yourArray.get(position).getTextFromFirst();
}
});
whre getTextFromFirst() is getter for your textview for example
How do i access the controls of an list view on item Click listener in a list view? As i have set the customize layout for a single list view item.
Attaching an item click listener
In the actvity where your listview is created try something similar to these
private void setupListView() {
//find by view id
customListView = ((ListView) findViewById(R.id.lv_listview);
//set custom adapter
customListView.setAdapter(
new CustomListAdapter(this, R.id.lv_listview, variableList));
//attach listener
customListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/* whatever happens when you click the list item goes here */
}
});
}