標籤: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輸入的值