Tengo un listview
funcionando con sqlite
dentro de un fragment
pero al querer obtener el id
de la lista con setOnItemClickListener
, no funciona.
Asi lo hago:
mClientesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
showSuccessMessage("El id es ");
try{
Cursor currentItem = (Cursor) mClienteAdapter.getItem(i);
String currentClienteId = currentItem.getString(
currentItem.getColumnIndex(contracts.clienterEntry.Cid));
showSuccessMessage("El id es "+ currentClienteId);
}catch (Exception e){
Log.e("ClickCliente", "onItemClick: ",e );
}
}
});
Pero no hace nada.
ACTUALIZACIÓN.
Al hacer un debug en la app, me muestra el siguiente mensaje:
Agrego el código de la clase completo
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();
Log.i("MClientes", "Prueba toas");
mClientesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
showSuccessMessage("El id es ");
try{
Cursor currentItem = (Cursor) mClienteAdapter.getItem(i);
String currentClienteId = currentItem.getString(currentItem.getColumnIndex(contracts.clienterEntry.Cid));
showSuccessMessage("El id es "+ currentClienteId);
}catch (Exception e){
Log.e("ClickCliente", "onItemClick: ",e );
}
}
});
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
}
}
}}
y este el layout
<FrameLayout 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="match_parent"
tools:context="com.cspm.ventas6.cspm.v_fragments.f_clientes"
android:focusable="false">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clientes"
android:focusable="false"/>
<ListView
android:id="@+id/cliente_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_btn_add_Cliente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="25dp"
android:layout_marginRight="25dp"
android:background="@color/colorPrimaryDark"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="@android:drawable/ic_input_add"
android:backgroundTint="@color/colorPrimaryDark" />
EL listview es llenado desde una tabla de un sqlite
Cursor currentItem = (Cursor) adapterView.getAdapter().getItem(i);
. Una pregunta, para entender más tu problema. ¿Depuraste el código para ver que pasaba?, ¿Te está lanzando la excepcion del catch?. En caso de que hayas pasado el cursor como parámetro a tu adapter intenta castearlo a tu clase del adaptador.