当前位置: 动力学知识库 > 问答 > 编程问答 >

Update a field in a listview - Android

问题描述:

I have a ListView and a spinner and when I want select from the spinner, I want to update one field for every item in the ListView with the selected value from the spinner.

I have a CustomAdapter which extends CursorAdapter. How can I do that exactly?

Code for CustomCursorAdapterProdus2:

public class CustomCursorAdapterProdus2 extends CursorAdapter {

private static final String TAG = null;

Context con;

public CustomCursorAdapterProdus2(Context context, Cursor c) {

super(context, c);

con = context;

}

@Override

public View newView(Context context, Cursor cursor, ViewGroup parent) {

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

View retView = inflater.inflate(R.layout.single_row_item_produsefactura, parent, false);

return retView;

}

@Override

public void bindView(View view, Context context, Cursor cursor) {

TextView textViewId = (TextView) view.findViewById(R.id.denumire);

textViewId.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

TextView textId = (TextView) view.findViewById(R.id.id_produsFact);

textId.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0))));

TextView textUm = (TextView) view.findViewById(R.id.unitateMasura);

textUm.setText(" (" + cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4))) + ")");

}

}

This is the code for single_row_item_produsefactura.xml which fills the listview:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/relativLayoutLista"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:padding="10sp"

android:background="#4A4A4A">

<!-- 0 -->

<TextView

android:id="@+id/spatiu"

android:layout_width="160dp"

android:layout_height="30dp"

android:textSize="16sp"

android:textColor="#FCFBFD"

android:text=""

/>

<!-- 1 -->

<TextView

android:id="@+id/cantitate"

android:layout_width="70dp"

android:layout_height="30dp"

android:textSize="16sp"

android:textColor="#FCFBFD"

android:text="Cantitate"

android:layout_toRightOf="@+id/spatiu"

/>

<!-- 2 -->

<TextView

android:id="@+id/pret"

android:layout_width="100dp"

android:layout_height="30dp"

android:textSize="16sp"

android:textColor="#FCFBFD"

android:text="Pret"

android:layout_toRightOf="@+id/unitateMasura"

/>

<!-- 3 -->

<TextView

android:id="@+id/denumire"

android:background="#2A2A2A"

android:layout_width="150dp"

android:layout_height="40dp"

android:paddingLeft="10dp"

android:textSize="22sp"

android:layout_marginRight="10dp"

android:textColor="#FCFBFD"

android:text="(denumire)"

android:layout_below="@+id/spatiu"

/>

<!-- 4 -->

<EditText

android:id="@+id/edit_cantitate"

android:layout_width="100dp"

android:layout_height="40dp"

android:textSize="20sp"

android:maxLength="7"

android:textColor="#FCFBFD"

android:text="0"

android:paddingLeft="5dp"

android:layout_toRightOf="@+id/denumire"

android:layout_below="@+id/cantitate"

android:inputType="numberDecimal"

android:clickable="true"

/>

<!-- 5 -->

<Button

android:id="@+id/butplus"

style="?android:attr/buttonStyleInset"

android:layout_width="55dp"

android:layout_height="40dp"

android:textAlignment="center"

android:textSize="16sp"

android:onClick="onClickGoPlus"

android:textColor="#FCFBFD"

android:layout_toRightOf="@+id/edit_cantitate"

android:layout_alignBottom="@+id/denumire"

android:text="+1">

</Button>

<!-- 6 -->

<Button

android:id="@+id/butplus10"

style="?android:attr/buttonStyleInset"

android:layout_width="55dp"

android:layout_height="40dp"

android:textAlignment="center"

android:textSize="16sp"

android:onClick="onClickGoPlus"

android:textColor="#FCFBFD"

android:layout_toRightOf="@+id/butplus"

android:layout_alignBottom="@+id/denumire"

android:text="+10">

</Button>

<!-- 7 -->

<Button

android:id="@+id/butminus"

style="?android:attr/buttonStyleInset"

android:layout_width="55dp"

android:layout_height="40dp"

android:textAlignment="center"

android:textSize="16sp"

android:onClick="onClickGoPlus"

android:textColor="#FCFBFD"

android:layout_toRightOf="@+id/butplus10"

android:layout_alignBottom="@+id/denumire"

android:text="-1">

</Button>

<!-- 8 -->

<Button

android:id="@+id/butminus10"

style="?android:attr/buttonStyleInset"

android:layout_width="55dp"

android:layout_height="40dp"

android:textAlignment="center"

android:textSize="16sp"

android:onClick="onClickGoPlus"

android:textColor="#FCFBFD"

android:layout_toRightOf="@+id/butminus"

android:layout_alignBottom="@+id/denumire"

android:text="-10">

</Button>

<!-- 9 -->

<EditText

android:id="@+id/edit_pret"

android:layout_width="100dp"

android:layout_height="40dp"

android:textSize="20sp"

android:textColor="#FCFBFD"

android:inputType="numberDecimal"

android:maxLength="7"

android:text="0"

android:paddingLeft="5dp"

android:layout_below="@+id/pret"

android:layout_alignBottom="@+id/denumire"

android:layout_toRightOf="@+id/butminus10"

android:clickable="true"

/>

<!-- 10 -->

<TextView

android:id="@+id/id_produsFact"

android:background="#2A2A2A"

android:padding="5dp"

android:layout_width="0dp"

android:layout_height="0dp"

android:textSize="16sp"

android:textColor="#FCFBFD"

android:text="(id_produsFact)"

android:layout_below="@+id/spatiu"/>

<!-- 11 -->

<TextView

android:id="@+id/valoare"

android:layout_width="90dp"

android:layout_height="40dp"

android:textSize="16sp"

android:textColor="#FCFBFD"

android:text="Valoare"

android:layout_toRightOf="@+id/pret"

/>

<!-- 12 -->

<TextView

android:id="@+id/valoaretva"

android:layout_width="130dp"

android:layout_height="40dp"

android:textSize="16sp"

android:textColor="#FCFBFD"

android:text="Valoare Tva"

android:layout_toRightOf="@+id/valoare"

/>

<!-- 13 -->

<TextView

android:id="@+id/tv_valoare"

android:layout_width="90dp"

android:layout_height="40dp"

android:textSize="20sp"

android:textColor="#FCFBFD"

android:maxLength="8"

android:text="0"

android:layout_toRightOf="@+id/edit_pret"

android:layout_below="@+id/valoare"

android:paddingBottom="5dp"

android:paddingLeft="5dp"

/>

<!-- 14 -->

<TextView

android:id="@+id/tv_valoaretva"

android:layout_width="130dp"

android:layout_height="40dp"

android:textSize="20sp"

android:maxLength="8"

android:textColor="#FCFBFD"

android:text="0"

android:layout_toRightOf="@+id/tv_valoare"

android:layout_below="@+id/valoaretva"

/>

<!-- 15 -->

<TextView

android:id="@+id/unitateMasura"

android:layout_width="250dp"

android:layout_height="30dp"

android:textSize="16sp"

android:textColor="#FCFBFD"

android:text="()"

android:layout_toRightOf="@+id/cantitate"

/>

</RelativeLayout>

分享给朋友:
您可能感兴趣的文章:
随机阅读: