服务器之家

服务器之家 > 正文

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

时间:2021-01-08 13:46     来源/作者:Android教程网

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; 

         } 

     } 

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部