0

tengo implementado un listview con sqlite, pero el evento setOnItemClickListener del listview no funciona, entonces quiero obtener la posicion de item, desde un ImagenView (img_btn_next_cliente) dentro de el item que infla al listview.

Este es le layout del 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" />

Esta es la clase adapter

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);
}

}

y este la actividad principal que es un 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
        }
    }

}

}

Como les comentaba, el evento setOnItemClickListener del listview no funciona, entonces quiero hacerlo de esta manera.

2
  • probaste view.setOnClickListener dentro de tu bindView?
    – Ivan Koop
    el 2 may. 2017 a las 2:07
  • No entiendo tu respuesta, puedes ayudarme con algo más detallado el 2 may. 2017 a las 15:52

1 respuesta 1

0
 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);
    View mView =(View) view.findViewById(R.id.indicator_status);
    String nombre = cursor.getString(cursor.getColumnIndex(contracts.clienterEntry.Cnombre));
    String negocio = cursor.getString(cursor.getColumnIndex(contracts.clienterEntry.Ccodigo));
    idC = cursor.getString(cursor.getColumnIndex(contracts.clienterEntry.Cid));
    long idlng=cursor.getLong(0);
    int_id=Integer.parseInt(idC);
    int_id=int_id-6;
    idC=""+idlng;
    nombrecliente.setText(nombre + "id:  "+idC);
    nombrelocal.setText(negocio);
    mView.setBackgroundColor(Color.RED);
    btn_next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("in view", "onClick id :"+idC);
            Intent intent = new Intent(context, ClienteDetalleActivity.class);
            intent.putExtra("clienteid", idC);
            context.startActivity(intent);
        }
    });
}

Al tomar el id, me devuelve con 6 mas de si posicion, es decir si id es 1 me devuelve6, pero si le resto 6, me devuelve negativos correctos, -5 -4 -3 dependiendo si la id es 1 2 3.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.