I have a listView with a button in each row. The problem is that I can click in my listView item and something happens, but my Button does not work, nothing happens. I've read some things about the solution, but none helped me.
Here's my list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="@+id/text_branche_cours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
/>
<TextView
android:id="@+id/text_trait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - "
/>
<TextView
android:id="@+id/text_designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading"
android:layout_marginRight="150dp"
/>
<Button
android:id="@+id/bAjouterJalon"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_add_jalon"
/>
And here's my customadapter class:
public class CustomAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
super.bindView(view, context, cursor);
final Button ajouter = (Button)view.findViewById(R.id.bAjouterJalon);
ajouter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent retour = new Intent(mContext,OngletJalonsNotes.class);
mContext.startActivity(retour);
}
});
}
}
Thank you for the help.
@EDIT - my listview is unclickable now !!
you are making button focusable & focus on touch false so it will not detect your touch tab on your button, remove these two lines from your button tag in xml :
android:focusable="false"
android:focusableInTouchMode="false"
Example:
<Button
android:id="@+id/bAjouterJalon"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_add_jalon"
/>
*****************New changes*******************
CustomAdapter.class:
public class CustomAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
super.bindView(view, context, cursor);
final Button ajouter = (Button)view.findViewById(R.id.bAjouterJalon);
final LinearLayout root_view = (LinearLayout)view.findViewById(R.id.item_root);
root_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// List item Click detected
}
});
ajouter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent retour = new Intent(mContext,OngletJalonsNotes.class);
mContext.startActivity(retour);
}
});
}
}
List item xml:
<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/item_root"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="@+id/text_branche_cours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="English" />
<TextView
android:id="@+id/text_trait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text=" - " />
<TextView
android:id="@+id/text_designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="150dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Reading" />
<Button
android:id="@+id/bAjouterJalon"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_add_jalon" />
</LinearLayout>