Android:列表视图中的数量按钮 - 仅更新热门数量

时间:2015-11-23 10:00:14

标签: java android listview android-studio

我创建了一个列表视图,每行上都有加号和减号按钮,但值只在顶行上发生变化我还需要一种方法,这样当我点击提交订单时,它会引用咖啡的类型并说出数量是多少是那个订购的。

activity_main.xml

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"></ListView>
</LinearLayout>

customListAdapter.java

public class CustomListAdapter extends ArrayAdapter<String> {
String[] description = {
        "Price: £1.75",
        "Price: £2.15",
        "Price: £2.80",
        "Price: £2.00",
        "Price: £2.80",
        "Price: £3.35",
        "Price: £3.15",
        "Price: £2.00",
        "Price: £2.45",
        "Price: £3.00",
        "Price: £2.25",
};

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
    super(context, R.layout.mylist, itemname);
    // TODO Auto-generated constructor stub

    this.context=context;
    this.itemname=itemname;
    this.imgid=imgid;
}



public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.mylist, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

    txtTitle.setText(itemname[position]);
    imageView.setImageResource(imgid[position]);
    extratxt.setText(description[position]);
    return rowView;

};

myList.xml

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:padding="2dp"
        android:text="Top text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textColor="#906B41"
        android:text="Bottom Text" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:orientation="horizontal">
    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </View>
    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:onClick="decrement"
        android:textAlignment="center"
        android:background="@layout/roundshapebtn"
        android:text="-" />

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="0"
        android:textColor="@android:color/white"
        android:textAlignment="center"
        android:textSize="18sp" />

    <Button
        android:id="@+id/increment_button_view"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:onClick="increment"
        android:textAlignment="center"
        android:text="+"
        android:background="@layout/roundshapebtn"/>
</LinearLayout>

Image showing the quantity only changes on the top value

0 个答案:

没有答案