安卓——ListView的動態添加與刪除

來源:互聯網
上載者:User

標籤: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的動態添加與刪除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.