欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android ListView内数据的动态添加与删除实例代码

程序员文章站 2023-11-29 09:37:34
main.xml 文件:  复制代码 代码如下:   &l...

main.xml 文件: 

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?> 

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 

     android:layout_width="fill_parent" 

     android:layout_height="fill_parent" 

     android:orientation="horizontal"  

     > 

     <linearlayout 

       android:layout_width="fill_parent" 

      android:layout_height="fill_parent"    

      android:orientation="vertical" 

      > 

     <listview  

      android:id="@+id/listview"     

      android:layout_width="fill_parent" 

      android:layout_height="wrap_content" 

     /> 

     <button  

      android:id="@+id/add"     

      android:layout_width="wrap_content" 

      android:layout_height="wrap_content"  

      android:text="添加" 

      /> 

     </linearlayout> 

 </linearlayout>


listview_item.xml文件:
复制代码 代码如下:

 <?xml version="1.0" encoding="utf-8"?> 

 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 

     android:layout_width="fill_parent" 

     android:layout_height="wrap_content" 

     android:orientation="horizontal" 

     android:background="#000000" 

     android:padding="20dp" 

     > 

        

     <edittext 

     android:id="@+id/edit" 

     android:layout_width="200dp" 

     android:layout_height="wrap_content" 

     /> 

     <button 

     android:id="@+id/del" 

     android:layout_width="wrap_content" 

     android:layout_height="wrap_content"    

     android:text="删除" 

     /> 

        

 </linearlayout>

mainactivity .java

复制代码 代码如下:

 package com.yyy.testandroid; 

    
import java.util.arraylist; 
    

import android.app.activity; 

import android.content.context; 

import android.os.bundle; 

import android.view.layoutinflater; 

import android.view.view; 

import android.view.view.onclicklistener; 

import android.view.view.onfocuschangelistener; 

import android.view.viewgroup; 

 import android.widget.baseadapter; 

 import android.widget.button; 

 import android.widget.edittext; 

 import android.widget.listview; 

 import android.widget.textview; 

    

 public class testandroidactivity extends activity { 

     /** called when the activity is first created. */ 

        

     private button button,add; 

     private textview text; 

     private listview listview; 

     public myadapter adapter; 

     @override 

     public void oncreate(bundle savedinstancestate) { 

         super.oncreate(savedinstancestate); 

         setcontentview(r.layout.main); 

         listview = (listview) findviewbyid(r.id.listview); 

         add = (button) findviewbyid(r.id.add); 

         adapter = new myadapter(this); 

         listview.setadapter(adapter); 

            

         add.setonclicklistener(new onclicklistener() { 

             @override 

             public void onclick(view arg0) { 

                 // todo auto-generated method stub 

                 adapter.arr.add(""); 

                 adapter.notifydatasetchanged(); 

             } 

         }); 

     } 
 

     private class myadapter extends baseadapter { 

    

         private context context; 

         private layoutinflater inflater; 

         public arraylist<string> arr; 

         public myadapter(context context) { 

             super(); 

             this.context = context; 

             inflater = layoutinflater.from(context); 

             arr = new arraylist<string>(); 

             for(int i=0;i<3;i++){    //listview初始化3个子项 

                 arr.add(""); 

             } 

         } 

         @override 

         public int getcount() { 

             // todo auto-generated method stub 

             return arr.size(); 

         } 

         @override 

         public object getitem(int arg0) { 

             // todo auto-generated method stub 

             return arg0; 

         } 

         @override 

         public long getitemid(int arg0) { 

             // todo auto-generated method stub 

             return arg0; 

         } 

         @override 

         public view getview(final int position, view view, viewgroup arg2) { 

             // todo auto-generated method stub 

             if(view == null){ 

                 view = inflater.inflate(r.layout.list_item, null); 

             } 

             final edittext edit = (edittext) view.findviewbyid(r.id.edit); 

             edit.settext(arr.get(position));    //在重构adapter的时候不至于数据错乱 

             button del = (button) view.findviewbyid(r.id.del); 

             edit.setonfocuschangelistener(new onfocuschangelistener() { 

                 @override 

                 public void onfocuschange(view v, boolean hasfocus) { 

                     // todo auto-generated method stub 

                     if(arr.size()>0){ 

                         arr.set(position, edit.gettext().tostring()); 

                     } 

                 } 

             }); 

             del.setonclicklistener(new onclicklistener() { 

                 @override 

                 public void onclick(view arg0) { 

                     // todo auto-generated method stub 

                     //从集合中删除所删除项的edittext的内容 

                     arr.remove(position); 

                     adapter.notifydatasetchanged(); 

                 } 

             }); 

             return view; 

         } 

     } 

 }