Insert a counter within the ListView

0

I'm using a CursorAdapter to populate my listView, dento of each item in my listView there are two buttons, one to add and one to decrease the value, and a textView to display this value, I wanted to know how to handle this.

item.xml xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_toStartOf="@id/valor"
    android:layout_toLeftOf="@+id/valor"
    android:layout_toEndOf="@id/amigo"
    android:layout_toRightOf="@id/amigo"
    android:gravity="center">

    <ImageButton
        android:id="@+id/botaomenos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menos"
        android:onClick="menos"
       />

    <TextView
        android:id="@+id/quantidade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:text="5"
        android:textColor="@color/Texto" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mais"
        android:onClick="mais"/>

</LinearLayout>

CursorAdapter

public class AdapterProduct extends CursorAdapter {

public AdapterProduto(Context context, Cursor c) {
    super(context, c, 0);
}

public static class Holder {
    TextView amigo;
    TextView valor;
    TextView quantidade;


    public Holder(View view) {
        amigo = view.findViewById(R.id.amigo);
        valor = view.findViewById(R.id.valor);
        quantidade = view.findViewById(R.id.quantidade);

    }
}

public void updateItens(String itens) {
    this.itens = itens;
    notifyDataSetChanged();
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    //pega a posição da view
    int viewType = getItemViewType(cursor.getPosition());

    //define o layout a ser selecionado
    int layoutId = -1;

    layoutId = R.layout.teste;

    View view = LayoutInflater.from(context).inflate(layoutId, viewGroup, false);


    Holder holder = new Holder(view);
    view.setTag(holder);

    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    Holder holder = (Holder) view.getTag();

    int amigoIndex = cursor.getColumnIndex(Contrato.ProdutoEntry.PRODUTO);
    int valorIndex = cursor.getColumnIndex(Contrato.ProdutoEntry.VALOR);

    holder.amigo.setText(cursor.getString(amigoIndex));
    holder.valor.setText("R$ " + cursor.getInt(valorIndex));

}

}

    
asked by anonymous 08.07.2018 / 04:49

1 answer

0

You can get the position of the listview in onclick, and you can add the positions in an array based on your position.

    
08.07.2018 / 05:23