I have implemented a listview with sqlite, but the setOnItemClickListener event of the listview does not work, so I want to obtain the item position, from an ImageView (img_btn_next_client) within the item that inflates to the listview.
This is the layout of the Item
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="70dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardUseCompatPadding="true"
android:focusable="false">
<!-- Indicador de estado -->
<View
android:id="@+id/indicator_appointment_status"
android:layout_width="8dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#99ff00"
android:focusable="false"
/>
<RelativeLayout
android:id="@+id/content_item_cliente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:paddingBottom="8dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp">
<TextView
android:id="@+id/text_nombre_negocio"
style="@style/Base.TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:focusable="false"
tools:text="Nombre del Negocio" />
<TextView
android:id="@+id/text_cliente_nombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/text_nombre_negocio"
android:layout_alignStart="@+id/text_nombre_negocio"
android:layout_below="@id/text_nombre_negocio"
android:focusable="false"
tools:text="Cliente: Jorge Ramos" />
</RelativeLayout>
<ImageView
android:id="@+id/img_btn_next_cliente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/text_nombre_negocio"
android:layout_gravity="right|center"
android:adjustViewBounds="false"
android:cropToPadding="false"
app:srcCompat="@drawable/ic_next_cliente" />
This is the adapter class
public class ClientesCursorAdapter extends CursorAdapter {
public ClientesCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.item_cliente, viewGroup, false);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
TextView nombrecliente = (TextView) view.findViewById(R.id.text_cliente_nombre);
TextView nombrelocal = (TextView) view.findViewById(R.id.text_nombre_negocio);
ImageView btn_next= (ImageView) view.findViewById(R.id.img_btn_next_cliente);
String nombre = cursor.getString(cursor.getColumnIndex(contracts.clienterEntry.Cnombre));
String negocio = cursor.getString(cursor.getColumnIndex(contracts.clienterEntry.Ccodigo));
String id = cursor.getString(cursor.getColumnIndex(contracts.clienterEntry.Cid));
nombrecliente.setText(nombre + "id:"+id);
nombrelocal.setText(negocio);
}
}
and this the main activity that is a fragment
public class f_clientes extends Fragment {
public static final int REQUEST_UPDATE_CLIENTE = 2;
private dbdata mDbCliente;
private ListView mClientesList;
private ClientesCursorAdapter mClienteAdapter;
private FloatingActionButton mAddCliente;
public f_clientes() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static f_clientes newInstance() {
return new f_clientes();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root= inflater.inflate(R.layout.fragment_f_clientes, container, false);
mClientesList=(ListView)root.findViewById(R.id.cliente_list);
mClienteAdapter=new ClientesCursorAdapter(getActivity(),null);
mClientesList.setAdapter(mClienteAdapter);
mDbCliente=new dbdata(getActivity());
loadClientes();
mClientesList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
Cursor currentItem = (Cursor) mClienteAdapter.getItem(i);
System.out.println("selected value >> >> >> >> >> >>"+currentItem);
}
});
FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.fab_btn_add_Cliente);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ClienteDetalleActivity.class);
intent.putExtra("clienteid", "1");
startActivity(intent);
}
});
return root;
}
private void loadClientes()
{
Log.i("MClientes", "cargando clientes...");
new ClientesLoadTask().execute();
}
private void showDetailScreen(String lawyerId) {
Intent intent = new Intent(getActivity(), ClienteDetalleActivity.class);
intent.putExtra(MainActivity.EXTRA_ClIENTE_ID, lawyerId);
startActivityForResult(intent, REQUEST_UPDATE_CLIENTE);
}
private void showSuccessMessage(String mensaje) {
Toast.makeText(getActivity(),
mensaje, Toast.LENGTH_SHORT).show();
}
private class ClientesLoadTask extends AsyncTask<Void, Void, Cursor> {
@Override
protected Cursor doInBackground(Void... voids) {
return mDbCliente.getAllClientes();
}
@Override
protected void onPostExecute(Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
mClienteAdapter.swapCursor(cursor);
} else {
// Mostrar empty state
}
}
}
}
As I was saying, the setOnItemClickListener event in listview does not work, so I want to do it this way.