Android-------ListView列表中擷取EditText輸入的值

來源:互聯網
上載者:User

標籤:main   fill   listener   create   擷取   app   start   技術分享   text   

最近項目的購物車中用列表中包含了留言功能, 需要擷取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) {        }    }}

 

代碼是寫的比較簡單,源碼點擊下載

 

Android-------ListView列表中擷取EditText輸入的值

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.