gpt4 book ai didi

android - 如何使用点击 ListView 中的项目来更新数量

转载 作者:行者123 更新时间:2023-11-30 03:39:42 25 4
gpt4 key购买 nike

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

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

中输入他们愿意的数量

常量.java::

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

最佳答案

改变你的方法如下:

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

}
});

关于android - 如何使用点击 ListView 中的项目来更新数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15984338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com