一,添加
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(){ @Override public 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() { @Override public void afterTextChanged(Editable s) { listItem.get(mPosition).put("BeginTime", s.toString()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } }); listItemView.txtTemperature.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable s) { listItem.get(mPosition).put("txtTemperature", s.toString()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } }); listItemView.imgDelete.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //Toast.makeText(context, mPosition+"", Toast.LENGTH_SHORT).show(); listItem.remove(mPosition); notifyDataSetChanged(); } }); return convertView; }
activity如下:
唉,真心感觉listView很奇葩(多于一屏的问题还没有处理掉,有经验的留个言)。