How to Fire Onlistitemclick in Listactivity with Buttons in List

onListItemClick method not called when I use Checkox in list

To make it working you can set Checkboxes and other fields in the ListItem unclickable and unfocusable, by properties:

android:clickable="false"
android:focusable="false"

how to setOnclickListener() on the Button inside the ListView?

just handle on click listener inside getview where you find the button using findviewbyid

this will handle the current row button click

public class MySimpleArrayAdapter extends ArrayAdapter<String> implements OnClickListener {
private final Activity context;
private final String[] names;
private Button deleteButton= null;
public MySimpleArrayAdapter(Activity context, String[] names) {
super (context, R.layout.imagelistlayout, names);
this.context = context;
this.names = names;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
deleteButton.setTag(position);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(names[position]);
deleteButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
//try to hide textview or something it may help
}
});
return rowView;

}

}`

Identify the item clicked on ListActivity method onListItemClick

What is the source of your list data? If you are using a cursor - then the id passed in onListItemClick(ListView l, View v, int position, long id) will automatically be the id of the cursor row.

ListActivity's onListItemClick does not work

Try to also set the CheckBox from the row layout as not focusable:

//...
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:focusable="false"
android:clickable="true">

//...

ListFragment with buttons for each row

For the problem with selecting the rows, I made it work. There is nothing wrong with the code I put before. After a lot of research I found this How to fire onListItemClick in Listactivity with buttons in list?

The comment that helped me was the one that says:

"There is even more elegant solution try to add android:descendantFocusability="blocksDescendants" in root layout of list element. That will make clicks onListItem possible and separately u can handle Button or ImageButton clicks".

I hope this could help to any one.



Related Topics



Leave a reply



Submit