Insert a counter into Listview

Asked

Viewed 84 times

0

I am using a Cursoradapter to fill my listview, within each item of 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 perform this manipulation.

xml of the.xml item

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

}

}

1 answer

0

You can take the listview position in onclick, and you can add the positions in an array based on its position.

  • But Adapter is handled by a cursor, not an array

Browser other questions tagged

You are not signed in. Login or sign up in order to post.