如何在一个ListView使用的项目点击更新数量 [英] How to update Quantity using Tap on Item in a ListView

查看:126
本文介绍了如何在一个ListView使用的项目点击更新数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序中,我需要通过在ListView用它点击可以更新项目的数量,我已经试了好几次,但仍然没能解决这个问题,请大家帮我::

CartAdapter.java ::

 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    // TODO自动生成方法存根
    查看VI = convertView;
    如果(convertView == NULL)
        VI = inflater.inflate(R.layout.listrow_cart,NULL);
        vi.setClickable(真);
        vi.setFocusable(真);
        vi.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            //这里我想提出code更新产品的数量
        }
    });

要的删除项目的,我使用低于code:

  mImgBtnDelete =(ImageButton的)六
            .findViewById(R.id.mImgBtnDelete);
    mImgBtnDelete.setOnClickListener(新OnClickListener(){        公共无效的onClick(视图v){
            // TODO自动生成方法存根            Constants.mItem_Detail.remove(位置);
            notifyDataSetChanged();
            意图mInViewCartRefresh =新意图(活动,
                    CartActivity.class);
            activity.startActivity(mInViewCartRefresh);
            activity.finish();        }
    });   HashMap的<字符串,字符串>项目=新的HashMap<字符串,字符串>();
    项目= Constants.mItem_Detail.get(位置);

listrow_activity.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =5DP
机器人:layout_marginLeft =10dp
机器人:layout_marginRight =10dp
机器人:layout_marginTop =5DP
机器人:背景=@绘制/ btn_background
机器人:比重=中心
机器人:方向=横向
机器人:填充=5dip><的TextView
    机器人:ID =@ + ID /总
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBaseline =@ + ID /数量
    机器人:layout_alignBottom =@ + ID /数量
    机器人:layout_alignLeft =@ + ID / TextView02
    机器人:文字=46.50
    机器人:文字颜色=#343438
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?<的TextView
    机器人:ID =@ + ID /数量
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBaseline =@ + ID /成本
    机器人:layout_alignBottom =@ + ID /成本
    机器人:layout_alignLeft =@ + ID / TextView01
    机器人:layout_marginLeft =22dp
    机器人:文字=3
    机器人:textAppearance =机器人:ATTR / textAppearanceSmall
    机器人:文字颜色=#343438/><的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_below =@ + ID /标题
    机器人:layout_marginLeft =30dp
    机器人:文字=成本
    机器人:textAppearance =机器人:ATTR / textAppearanceSmall
    机器人:文字颜色=#343434
    机器人:文字样式=大胆/><的TextView
    机器人:ID =@ + ID / TextView01
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID /成本
    机器人:layout_centerHorizo​​ntal =真
    机器人:文字=数量
    机器人:textAppearance =机器人:ATTR / textAppearanceSmall
    机器人:文字颜色=#343434
    机器人:文字样式=大胆/><的TextView
    机器人:ID =@ + ID /标题
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:文字=项目名称1
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium
    机器人:文字颜色=#a60704/><的TextView
    机器人:ID =@ + ID / TextView02
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentRight =真
    机器人:layout_below =@ + ID /标题
    机器人:layout_marginRight =30dp
    机器人:文字=总计
    机器人:textAppearance =机器人:ATTR / textAppearanceSmall
    机器人:文字颜色=#343434
    机器人:文字样式=大胆/><的TextView
    机器人:ID =@ + ID /成本
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignLeft =@ + ID / textView1
    机器人:layout_below =@ + ID / textView1
    机器人:文字=15.50
    机器人:textAppearance =机器人:ATTR / textAppearanceSmall
    机器人:文字颜色=#343438/><的ImageButton
    机器人:ID =@ + ID / mImgBtnDelete
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignLeft =@ + ID / TextView01
    机器人:layout_below =@ + ID /数量
    机器人:可聚焦=假
    机器人:背景=@绘制/ remove_btn/>  < / RelativeLayout的>

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

Constants.java ::

 公共类常量{
公共静态的ArrayList<&HashMap的LT;字符串,字符串>> mItem_Detail =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    }


解决方案

更改方法如下:

 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    // TODO自动生成方法存根
    查看VI = convertView;
    如果(convertView == NULL)
        VI = inflater.inflate(R.layout.listrow_cart,NULL);
        vi.setClickable(真);
        vi.setFocusable(真);
        vi.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            HashMap的<字符串,字符串>项目= mItem_Detail.get(位置);
            item.put(KEY_TO_BE_CHANGED,为newValue);
            mItem_Detail.set(位置,项目);
            notifyDataSetInvalidated();        }
    });

I am writing an App in which i need to update Quantity of an Item by using Tap on it in a ListView, i have tried several times but still not able to resolve this issue, please help me::

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

To delete an item, i am using below code:

   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>

Note: I am allowing user to enter their willing quantity in Items.java

Constants.java::

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

解决方案

Change your method as below:

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

        }
    });

这篇关于如何在一个ListView使用的项目点击更新数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆