如何使用TapView中的项目更新数量

时间:2013-04-13 04:44:43

标签: android android-listview

我正在编写一个应用程序,其中我需要通过在ListView中使用Tap来更新项目的数量,我已经尝试了几次但仍然无法解决此问题,请帮助我::

CartAdapter.java ::

 public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.listrow_cart, null);
        vi.setClickable(true);
        vi.setFocusable(true);
        vi.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //here i want to put code to update product quantity
        }
    });

删除项目,我使用以下代码:

   mImgBtnDelete = (ImageButton) vi
            .findViewById(R.id.mImgBtnDelete);
    mImgBtnDelete.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Constants.mItem_Detail.remove(position);
            notifyDataSetChanged();
            Intent mInViewCartRefresh = new Intent(activity,
                    CartActivity.class);
            activity.startActivity(mInViewCartRefresh);
            activity.finish();

        }
    });

   HashMap<String, String> item = new HashMap<String, String>();
    item = Constants.mItem_Detail.get(position);

listrow_activity.xml:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/btn_background"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
    android:id="@+id/total"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/qty"
    android:layout_alignBottom="@+id/qty"
    android:layout_alignLeft="@+id/TextView02"
    android:text="46.50"
    android:textColor="#343438"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/qty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/cost"
    android:layout_alignBottom="@+id/cost"
    android:layout_alignLeft="@+id/TextView01"
    android:layout_marginLeft="22dp"
    android:text="3"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#343438" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title"
    android:layout_marginLeft="30dp"
    android:text="Cost"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#343434"
    android:textStyle="bold" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/cost"
    android:layout_centerHorizontal="true"
    android:text="Quantity"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#343434"
    android:textStyle="bold" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Item Name 1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#a60704" />

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/title"
    android:layout_marginRight="30dp"
    android:text="Total"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#343434"
    android:textStyle="bold" />

<TextView
    android:id="@+id/cost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="15.50"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#343438" />

<ImageButton
    android:id="@+id/mImgBtnDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView01"
    android:layout_below="@+id/qty"
    android:focusable="false"
    android:background="@drawable/remove_btn" />

  </RelativeLayout>

注意:我允许用户在 Items.java中输入他们愿意的数量

Constants.java ::

   public class Constants {
public static ArrayList<HashMap<String, String>> mItem_Detail = new ArrayList<HashMap<String, String>>();
    }

2 个答案:

答案 0 :(得分:1)

更改您的方法如下:

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.listrow_cart, null);
        vi.setClickable(true);
        vi.setFocusable(true);
        vi.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            HashMap<String, String> item= mItem_Detail.get(position);
            item.put(KEY_TO_BE_CHANGED, newValue);
            mItem_Detail.set(position, item);
            notifyDataSetInvalidated();

        }
    });

答案 1 :(得分:0)

intent.putExtra("test", mItem_Detail);方法第一个参数是值的名称,后跟对象。