如何将侦听器添加到android中的listview适配器内的微调器中

时间:2017-09-11 10:06:38

标签: android listview spinner

enter image description here

public class XpressGoldLoanAdapter extends ArrayAdapter<TransactionDetailsUserData> {

        private List<TransactionDetailsUserData>list;
        private Context context;
        String securityNo;

        public XpressGoldLoanAdapter( List<TransactionDetailsUserData> list, Context ctx) {
            super(ctx, R.layout.reminderinflater, list);
            this.list = list;
            this.context = ctx;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public TransactionDetailsUserData getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return list.indexOf(getItem(position));
        }

        private  class ViewHolder{

            TextView tv_securityNo;
            Spinner spin_securityType,spin_security,spin_purityconfirmed ;
            LinearLayout sublayout;
            EditText ed_numbers,ed_grossWeight,ed_netWeight,ed_rate_perGram,ed_branchMarginRate,ed_amount;
            Button btn_savesecurity;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = convertView;
            Spinner spin_securityType,spin_security,spin_purityconfirmed;
            TextView tv_securityNo;
            EditText ed_numbers,ed_grossWeight,ed_netWeight,ed_rate_perGram,ed_branchMarginRate,ed_amount;
            LinearLayout sublayout;
            Button btn_savesecurity;

            ViewHolder holder = new ViewHolder();

            if (convertView == null) {
                LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.expressloanitem,null);

                spin_securityType       =(Spinner) view.findViewById(R.id.spin_securityType);
                spin_security           =(Spinner) view.findViewById(R.id.spin_security);
                spin_purityconfirmed    =(Spinner) view.findViewById(R.id.spin_purityconfirmed);
                tv_securityNo           =(TextView)view.findViewById(R.id.tv_securityNo);
                ed_numbers              =(EditText)view.findViewById(R.id.ed_numbers);
                ed_grossWeight          =(EditText)view.findViewById(R.id.ed_grossWeight);
                ed_netWeight            =(EditText)view.findViewById(R.id.ed_netWeight);
                ed_rate_perGram         =(EditText)view.findViewById(R.id.ed_rate_perGram);
                ed_branchMarginRate     =(EditText)view.findViewById(R.id.ed_branchMarginRate);
                ed_amount               =(EditText)view.findViewById(R.id.ed_amount);
                sublayout               =(LinearLayout)view.findViewById(R.id.sublayout);
                btn_savesecurity         =(Button) view.findViewById(R.id.btn_savesecurity);

                holder.spin_securityType = spin_securityType;
                holder.spin_security = spin_security;
                holder.spin_purityconfirmed = spin_purityconfirmed;
                holder.tv_securityNo = tv_securityNo;
                holder.ed_numbers = ed_numbers;
                holder.ed_grossWeight = ed_grossWeight;
                holder.ed_netWeight = ed_netWeight;
                holder.ed_rate_perGram = ed_rate_perGram;
                holder.ed_branchMarginRate = ed_branchMarginRate;
                holder.ed_amount = ed_amount;
                holder.sublayout = sublayout;
                holder.btn_savesecurity = btn_savesecurity;
                view.setTag(holder);
            }
            else{
                holder =(ViewHolder) view.getTag();
            }

            TransactionDetailsUserData pojo = enqList.get(position);
            securityNo = pojo.getSecurityCount();
            Log.d("securityNo",securityNo);
            holder.tv_securityNo.setText(securityNo);

            SpinnerLogoAdapter spinnerAdapter3=new SpinnerLogoAdapter(XpressGoldLoan.this.getApplicationContext(),securityTpeIcons,securityTpeitems);
            holder.spin_securityType.setAdapter(spinnerAdapter3);
            SpinnerLogoAdapter spinnerAdapter4=new SpinnerLogoAdapter(XpressGoldLoan.this.getApplicationContext(),securityIcons,securityitems);
            holder.spin_security.setAdapter(spinnerAdapter4);
            SpinnerLogoAdapter spinnerAdapter5=new SpinnerLogoAdapter(XpressGoldLoan.this.getApplicationContext(),purityIcons,purityitems);
            holder.spin_purityconfirmed.setAdapter(spinnerAdapter5);


            //spinner security type


            holder.spin_securityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });


            //save security
            holder.btn_savesecurity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            return view;

        }
    }

这是我的listview的adpater。当itry在监听器中添加spinner.getSelectedItem()时它会显示一些errors.App在spI的onItemSelected内崩溃。请找一个解决方案。我只需要从微调器中获取所选的项目我没有太多要详述我的问题,因为它很容易理解。请帮助

3 个答案:

答案 0 :(得分:0)

将您的持有人更改为最终(正如错误告诉您的那样)

final ViewHolder holder;

if (convertView == null) {
    holder = new ViewHolder()     
    ... //the rest of code here stays the same
} else {
    holder =(ViewHolder) view.getTag();
}

答案 1 :(得分:0)

在onItemselectedListener

中添加此行
holder.spin_securityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final AdapterView<?> parent,final View view,final int position,final long id) {
                 getSelectedItem(parent, view, position, id);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

在XpressGoldLoanAdapter类中再编写一个方法,它可以为您完成任务。

final void getSelectedItem(AdapterView<?> parent, View view, int position, long id) {
    String selectedItem = holder.spin_securityType.getSelectedItem().toString();
     // do your task here
}

答案 2 :(得分:-1)

我解决了它尝试此代码,因为securityTpeitems []是一个字符串数组,我已经从微调器获取位置。 字符串selectedSecType = securityTpeitems [position] .toString();