android ListView内数据的动态添加与删除


main.xml 文件:
01 <?xml version="1.0" encoding="utf-8"?> 
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03     android:layout_width="fill_parent" 
04     android:layout_height="fill_parent" 
05     android:orientation="horizontal"  
06     
07     <LinearLayout 
08       android:layout_width="fill_parent" 
09      android:layout_height="fill_parent"    
10      android:orientation="vertical" 
11      
12     <ListView  
13      android:id="@+id/listview"     
14      android:layout_width="fill_parent" 
15      android:layout_height="wrap_content" 
16     /> 
17     <Button  
18      android:id="@+id/add"     
19      android:layout_width="wrap_content" 
20      android:layout_height="wrap_content"  
21      android:text="添加" 
22      /> 
23     </LinearLayout
24 </LinearLayout>
listview_item.xml文件:
01 <?xml version="1.0" encoding="utf-8"?> 
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03     android:layout_width="fill_parent" 
04     android:layout_height="wrap_content" 
05     android:orientation="horizontal" 
06     android:background="#000000" 
07     android:padding="20dp" 
08     
09        
10     <EditText 
11     android:id="@+id/edit" 
12     android:layout_width="200dp" 
13     android:layout_height="wrap_content" 
14     /> 
15     <Button 
16     android:id="@+id/del" 
17     android:layout_width="wrap_content" 
18     android:layout_height="wrap_content"    
19     android:text="删除" 
20     /> 
21        
22 </LinearLayout>
MainActivity .java
001 package com.yyy.testandroid; 
002    
003    
004    
005 import java.util.ArrayList; 
006    
007 import android.app.Activity; 
008 import android.content.Context; 
009 import android.os.Bundle; 
010 import android.view.LayoutInflater; 
011 import android.view.View; 
012 import android.view.View.OnClickListener; 
013 import android.view.View.OnFocusChangeListener; 
014 import android.view.ViewGroup; 
015 import android.widget.BaseAdapter; 
016 import android.widget.Button; 
017 import android.widget.EditText; 
018 import android.widget.ListView; 
019 import android.widget.TextView; 
020    
021 public class TestAndroidActivity extends Activity { 
022     /** Called when the activity is first created. */ 
023        
024     private Button button,add; 
025     private TextView text; 
026     private ListView listview; 
027     public MyAdapter adapter; 
028     @Override 
029     public void onCreate(Bundle savedInstanceState) { 
030         super.onCreate(savedInstanceState); 
031         setContentView(R.layout.main); 
032         listview = (ListView) findViewById(R.id.listview); 
033         add = (Button) findViewById(R.id.add); 
034         adapter = new MyAdapter(this); 
035         listview.setAdapter(adapter); 
036            
037         add.setOnClickListener(new OnClickListener() { 
038             @Override 
039             public void onClick(View arg0) { 
040                 // TODO Auto-generated method stub 
041                 adapter.arr.add(""); 
042                 adapter.notifyDataSetChanged(); 
043             
044         }); 
045     
046        
047        
048     private class MyAdapter extends BaseAdapter { 
049    
050         private Context context; 
051         private LayoutInflater inflater; 
052         public ArrayList<String> arr; 
053         public MyAdapter(Context context) { 
054             super(); 
055             this.context = context; 
056             inflater = LayoutInflater.from(context); 
057             arr = new ArrayList<String>(); 
058             for(int i=0;i<3;i++){    //listview初始化3个子项 
059                 arr.add(""); 
060             
061         
062         @Override 
063         public int getCount() { 
064             // TODO Auto-generated method stub 
065             return arr.size(); 
066         
067         @Override 
068         public Object getItem(int arg0) { 
069             // TODO Auto-generated method stub 
070             return arg0; 
071         
072         @Override 
073         public long getItemId(int arg0) { 
074             // TODO Auto-generated method stub 
075             return arg0; 
076         
077         @Override 
078         public View getView(final int position, View view, ViewGroup arg2) { 
079             // TODO Auto-generated method stub 
080             if(view == null){ 
081                 view = inflater.inflate(R.layout.list_item, null); 
082             
083             final EditText edit = (EditText) view.findViewById(R.id.edit); 
084             edit.setText(arr.get(position));    //在重构adapter的时候不至于数据错乱 
085             Button del = (Button) view.findViewById(R.id.del); 
086             edit.setOnFocusChangeListener(new OnFocusChangeListener() { 
087                 @Override 
088                 public void onFocusChange(View v, boolean hasFocus) { 
089                     // TODO Auto-generated method stub 
090                     if(arr.size()>0){ 
091                         arr.set(position, edit.getText().toString()); 
092                     
093                 
094             }); 
095             del.setOnClickListener(new OnClickListener() { 
096                 @Override 
097                 public void onClick(View arg0) { 
098                     // TODO Auto-generated method stub 
099                     //从集合中删除所删除项的EditText的内容 
100                     arr.remove(position); 
101                     adapter.notifyDataSetChanged(); 
102                 
103             }); 
104             return view; 
105         
106     
107 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值