標籤:android listview
一,添加
case R.id.imgBtnAdd:/*添加列表*/if(SetTempListView.getChildCount()>5){Toast.makeText(getApplicationContext(), "列表只能添加6條!", Toast.LENGTH_SHORT).show();break;}listItem.clear();for(int i=0;i<SetTempListView.getChildCount();i++){View view=SetTempListView.getChildAt(i);TextView txtTemp=(TextView)view.findViewById(R.id.txtBeginTime);EditText editTemp=(EditText)view.findViewById(R.id.txtTemperature);HashMap<String,String> map=new HashMap<String,String>(); map.put("BeginTime",txtTemp.getText().toString()); map.put("txtTemperature", editTemp.getText().toString()); listItem.add(map);}HashMap<String,String> map1=new HashMap<String,String>(); map1.put("BeginTime","-請選擇時間-"); map1.put("txtTemperature",""); listItem.add(map1); listItemAdapter.notifyDataSetChanged();break;
二,刪除
listItemView.imgDelete.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {//Toast.makeText(context, mPosition+"", Toast.LENGTH_SHORT).show();listItem.remove(mPosition);notifyDataSetChanged();}});
三,修改後的getView方法
public View getView(final int mPosition, View convertView, ViewGroup parent) {//final int mPosition = position; ListItemView listItemView = null; //if (convertView == null) {convertView = listContainer.inflate(resource, null);//載入布局listItemView = new ListItemView();/*初始化控制項容器集合*/listItemView.txtBeginTime=(TextView) convertView.findViewById(R.id.txtBeginTime); /*開始時間*/listItemView.txtTemperature=(EditText)convertView.findViewById(R.id.txtTemperature);/*溫度顯示*/listItemView.imgDelete=(ImageView)convertView.findViewById(R.id.imgDelete);/*刪除此行*/// 設定控制項集到convertView //convertView.setTag(listItemView);//}else{//listItemView=(ListItemView)convertView.getTag();//利用緩衝的View//}listItemView.txtBeginTime.setText(listItem.get(mPosition).get("BeginTime"));listItemView.txtTemperature.setText(listItem.get(mPosition).get("txtTemperature"));//listItemView.txtBeginTime.setText(listItem.get(mPosition).get("BeginTime"));/*控制項的初始化操作*/new PopDateTimePicker(listItemView.txtBeginTime);/*綁定textchange事件,方式getview初始化時候丟掉資料*/listItemView.txtBeginTime.addTextChangedListener(new TextWatcher() {@Overridepublic void afterTextChanged(Editable s) {listItem.get(mPosition).put("BeginTime", s.toString());}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}});listItemView.txtTemperature.addTextChangedListener(new TextWatcher(){@Overridepublic void afterTextChanged(Editable s) {listItem.get(mPosition).put("txtTemperature", s.toString());}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}});listItemView.imgDelete.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {//Toast.makeText(context, mPosition+"", Toast.LENGTH_SHORT).show();listItem.remove(mPosition);notifyDataSetChanged();}});return convertView;}
activity如下:
唉,真心感覺listView很奇葩(多於一屏的問題還沒有處理掉,有經驗的留個言)。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
安卓——ListView的動態添加與刪除