• Android-------ListView列表中获取EditText输入的值


    最近项目的购物车中用列表中包含了留言功能, 需要获取EditText输入的内容,当购买多件商品时,就有点棘手了。

    经过查资料解决了这个功能,并写了一个案例;

       效果图:

        

     可以在商品数据用一个字段来管理留言数据,这样就可以方便的操作了。

     代码:

    public class MainActivity extends AppCompatActivity {
    
        ListView listView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView(){
            listView=(ListView)findViewById(R.id.listview);
            listView.setAdapter(new ListAdapter(this));
        }
        public void saveEditData(int position, String str) {
            Toast.makeText(this,str+"----"+position,Toast.LENGTH_LONG).show();
        }
    }

    Adapter

    public class ListAdapter extends BaseAdapter {
    
    
        Context context;
        LayoutInflater inflater;
        String[] strings=new String[]{"商品1","商品2","商品3"};
        public ListAdapter(Context context) {
            this.inflater=LayoutInflater.from(context);
            this.context=context;
        }
    
        @Override
        public int getCount() {
            return 3;
        }
    
        @Override
        public Object getItem(int position) {
            return strings[position];
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView){
                convertView = inflater.inflate(R.layout.item_multiple_confirm,null);
                holder =new ViewHolder(convertView,position);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv_name.setText(getItem(position).toString());
            return convertView;
        }
    
        class ViewHolder{
            TextView tv_name;
            EditText editText;
            public ViewHolder(View view,int pisition){
                tv_name = (TextView) view.findViewById(R.id.fill_order_name);
                editText= (EditText) view.findViewById(R.id.fill_order_ltext);
                editText.setTag(pisition);//存tag值
                editText.addTextChangedListener(new TextSwitcher(this));
            }
        }
    
        class TextSwitcher implements TextWatcher {
            private ViewHolder mHolder;
    
            public TextSwitcher(ViewHolder mHolder) {
                this.mHolder = mHolder;
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int position = (int) mHolder.editText.getTag();//取tag值
                ((MainActivity)context).saveEditData(position, s.toString());
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        }
    
    }

    代码是写的比较简单源码点击下载

  • 相关阅读:
    你要的SSM(Spring+Springmvc+Mybatis)小项目来了!!!
    王爽《汇编语言》(第三版)实验10解析
    java1.8安装及环境变量配置
    王爽《汇编语言》(第三版)实验9解析
    王爽《汇编语言》(第三版)实验8解析(超详细)
    2020软件工程作业06
    鸽子开发组——冲刺日志(第四天)
    String 类中常用方法
    mysql
    array_merge和加号+的区别
  • 原文地址:https://www.cnblogs.com/zhangqie/p/7526853.html
Copyright © 2020-2023  润新知