如何在listView中处理多个单击侦听器

时间:2017-05-17 06:02:47

标签: java android xml

这是我的适配器代码:

public static void main(String[] args) {
    // Insert data to database
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");       
    EntityManager em1 = emf.createEntityManager();          
    CnTermsBudget budget = new CnTermsBudget();
    CnTermsCounted counted = new CnTermsCounted();

    CnTermsGroup group = new CnTermsGroup();
    group.getCnTermsBudgets().add(budget);
    group.getCnTermsCounteds().add(counted);

    budget.setCnTermsGroup(group);
    counted.setCnTermsGroup(group);

    EntityTransaction t1 = em1.getTransaction();
    t1.begin();

    Long id = em1.merge(group).getId();

    t1.commit();
    em1.close();

    //Create transient entity 
    CnTermsAmortization amortization = new CnTermsAmortization();           

    EntityManager em = emf.createEntityManager();

    EntityTransaction t = em.getTransaction();
    t.begin();

    //Read persistent graph 
    group = em.find(CnTermsGroup.class, id); 

    budget = group.getCnTermsBudgets().get(0);  
    counted = group.getCnTermsCounteds().get(0);

    //Add transient entity to persistent tree
    budget.getAmortizations().add(amortization);
    counted.getAmortizations().add(amortization);

    amortization.setCnTermsBudget(budget);
    amortization.setCnTermsCounted(counted);

    //merge on the root
    em.merge(group);

    t.commit();

    em.close(); 



    em = emf.createEntityManager();

    List<?> ls = em.createQuery("from CnTermsAmortization").getResultList();
    System.out.println(Arrays.toString(ls.toArray()));

    em.close();
    emf.close();
}

如何制作我的自定义布局中两个按钮的onclicklist?

这是我的自定义布局XML:

 package com.app.beautysaloon;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.app.beautysaloon.Models.Orders;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.*;


public class CheckOutAdapter extends ArrayAdapter<Orders> {

    int Current_quantity;
    int updated_quantity;
    List<Orders> i_List;
    LayoutInflater inflater;
    Context context123;
    ViewHolder holder;

    public CheckOutAdapter(Context context, List<Orders>Orders ) {


        super(context, R.layout.checkout_screen_for_addition,Orders
        );
        // TODO Auto-generated constructor stub
        context123 = context;
        i_List = Orders;
    }

    public class ViewHolder {
        TextView item_name, item_quantity, item_description, item_price;
        Button add_btn, subtract_btn;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            inflater = LayoutInflater.from(context123);
            convertView = inflater.inflate
                    (R.layout.checkout_screen_for_addition, null);
        }

        holder = new ViewHolder();

        holder.item_name = (TextView) convertView.findViewById
                (R.id.item_name_tv);
        holder.item_quantity = (TextView) convertView.findViewById
                (R.id.item_quantity_tv);
        holder.item_description = (TextView) convertView.findViewById
                (R.id.description_tv_edt);
        holder.item_price = (TextView) convertView.findViewById
                (R.id.price_tv_edt);
        holder.add_btn = (Button) convertView.findViewById
                (R.id.add_btn);
        holder.subtract_btn = (Button) convertView.findViewById
                (R.id.subtract_btn);

        holder.item_name.setText(i_List.get(position).getItem_name());
        holder.item_quantity.setText(""+i_List.get(position).getItem_quantity());
        holder.item_description.setText(i_List.get(position).getItem_description());
        holder.item_price.setText("Rs." + i_List.get(position).getItem_price());

        holder.add_btn.setOnClickListener();

        return convertView;
    }
}

That is my custom layout image, I want to make on click listener of every button of "+" and "-" in every item of listview.

这是我的自定义布局图像,我想在列表视图的每个项目中点击“+”和“ - ”按钮的点击监听器。

请帮助我,我非常感谢你。

7 个答案:

答案 0 :(得分:0)

您可以按照以下方式执行此操作:

sed -r 's|[ ([0-9]+).|\1|g'

我假设您只需要使用该按钮完成操作,这可以在onclicklistner中完成。

答案 1 :(得分:0)

您可以在onCrate方法

中添加它
final Button button = (Button) findViewById(R.id.subtract_btn);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Subtracting code
         }
     });

final Button button = (Button) findViewById(R.id.add_btn);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             //Adding Code
         }
     });

答案 2 :(得分:0)

添加此代码以处理点击:

holder.add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {
               //your code here 
            }
        });

        holder.subtract_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                //your code here

            }
        });

但是如果你想保持它的值你可以像这样设置标记到那个视图:

holder.add_btn.setTag(R.string.tag_one,item_quantity_tv.getText());
holder.add_btn.setTag(R.string.tag_two,item_quantity_tv);

你可以在点击列表器中使用它:

    holder.add_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) 
                {
                   int iVal=(int)v.getTag(R.string.tag_one);
                   iVal++;
                   TextView txtvwVal=(TextView )v.getTag(R.string.tag_two);
                   txtvwVal.setText(iVal);
                   v.setTag(R.string.tag_one,iVal);
                }
            });

holder.subtract_btn.setTag(R.string.tag_one,item_quantity_tv.getText());
holder.subtract_btn.setTag(R.string.tag_two,item_quantity_tv);

holder.subtract_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                   int iVal=(int)v.getTag(R.string.tag_one);
                   iVal--;
                   TextView txtvwVal=(TextView )v.getTag(R.string.tag_two);
                   txtvwVal.setText(iVal);
                   v.setTag(R.string.tag_one,iVal);
                }
            });

如果我弄错了,这只是你问题的示例。 注意:在string.xml中声明它

<string name="tag_one">1</string>
<string name="tag_two">2</string>

答案 3 :(得分:0)

首先在构造函数中只对视图进行一次膨胀。目前,您每次都在夸大您的观看次数,而不是像这样重复使用

  public CheckOutAdapter(Context context, List<Orders>Orders ) {


    super(context, R.layout.checkout_screen_for_addition,Orders
    );
    // TODO Auto-generated constructor stub
    context123 = context;
    inflater = LayoutInflater.from(context);
    i_List = Orders;
}

然后

holder.add_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            i_List.get(position).setItem_quantity() = i_List.get(position).getItem_quantity()+1;
            holder.item_quantity.setText(""+ quantity);
        }
    });

    holder.subtract_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
             i_List.get(position).setItem_quantity() = i_List.get(position).getItem_quantity()-1;
            holder.item_quantity.setText(""+ quantity);

        }
    });

答案 4 :(得分:0)

设置android:focusable =&#34; false&#34;     机器人:focusableInTouchMode =&#34;假&#34;按钮 在自定义适配器布局中,然后为每个按钮应用单击侦听器

答案 5 :(得分:0)

以上所有答案都是正确的,这里的关键是 notifyDataSetChanged()方法

holder.add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {
               String newValue =i_List.get(position).getItem_quantity()+1;
               holder.item_quantity.setText(""+newValue);
               i_List.get(position).setItem_quantity(newValue);
               notifyDataSetChanged();
            }
        });

        holder.subtract_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
               String newValue =i_List.get(position).getItem_quantity()-1;
               holder.item_quantity.setText(""+newValue);
               i_List.get(position).setItem_quantity(newValue);
               notifyDataSetChanged();

            }
        });

notifyDataSetChanged()将使用新值刷新适配器

答案 6 :(得分:0)

    package com.app.beautysaloon;


import com.app.beautysaloon.Models.Orders;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.*;


public class CheckOutAdapter extends ArrayAdapter<Orders> {


    List<Orders> i_List;
    LayoutInflater inflater;
    Context context123;
    ViewHolder holder;

    public CheckOutAdapter(Context context, List<Orders>Orders ) {


        super(context, R.layout.checkout_screen_for_addition,Orders
        );
        // TODO Auto-generated constructor stub
        context123 = context;
        i_List = Orders;


        context123 = context;
        inflater = LayoutInflater.from(context);
        i_List = Orders;
    }

    public class ViewHolder {
        TextView item_name, item_quantity, item_description, item_price;
        Button add_btn, subtract_btn;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            inflater = LayoutInflater.from(context123);
            convertView = inflater.inflate
                    (R.layout.checkout_screen_for_addition, null);
        }

        holder = new ViewHolder();

        holder.item_name = (TextView) convertView.findViewById
                (R.id.item_name_tv);
        holder.item_quantity = (TextView) convertView.findViewById
                (R.id.item_quantity_tv);
        holder.item_description = (TextView) convertView.findViewById
                (R.id.description_tv_edt);
        holder.item_price = (TextView) convertView.findViewById
                (R.id.price_tv_edt);
        holder.add_btn = (Button) convertView.findViewById
                (R.id.add_btn);
        holder.subtract_btn = (Button) convertView.findViewById
                (R.id.subtract_btn);

        holder.item_name.setText(i_List.get(position).getItem_name());
        holder.item_quantity.setText(""+i_List.get(position).getItem_quantity());
        holder.item_description.setText(i_List.get(position).getItem_description());
        holder.item_price.setText("Rs." + i_List.get(position).getItem_price());

        //Creating Click listner of every item
        holder.add_btn.setOnClickListener(new OnItemClickListener(position, holder.item_quantity,"+"));
        holder.subtract_btn.setOnClickListener(new OnItemClickListener(position, holder.item_quantity,"-"));

        return convertView;
    }

    //That is the class which use to change value on Item click listner.

    private class OnItemClickListener  implements  View.OnClickListener {

        private int mPosition;
        private TextView tv;
        String sin;

        OnItemClickListener(int position,TextView tvs,String sing){
            mPosition = position;
            tv=tvs;
            sin=sing;
        }

        @Override
        public void onClick(View v) {
            int q= CheckOut.orders_list.get(mPosition).getItem_quantity();
            if(sin.equals("+")){
                q++;
            }else if(sin.equals("-")){
                q--;
            }
            CheckOut.orders_list.get(mPosition).setItem_quantity(q);
            tv.setText(""+q);

        }
    }
}