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


    main.xml 文件:

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="horizontal"   
    6.     >  
    7.     <LinearLayout  
    8.       android:layout_width="fill_parent"  
    9.      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文件:

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="wrap_content"  
    5.     android:orientation="horizontal"  
    6.     android:background="#000000"  
    7.     android:padding="20dp"  
    8.     >  
    9.       
    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

    [java] view plaincopy
     
      1. package com.yyy.testandroid;  
      2.   
      3.   
      4.   
      5. import java.util.ArrayList;  
      6.   
      7. import android.app.Activity;  
      8. import android.content.Context;  
      9. import android.os.Bundle;  
      10. import android.view.LayoutInflater;  
      11. import android.view.View;  
      12. import android.view.View.OnClickListener;  
      13. import android.view.View.OnFocusChangeListener;  
      14. import android.view.ViewGroup;  
      15. import android.widget.BaseAdapter;  
      16. import android.widget.Button;  
      17. import android.widget.EditText;  
      18. import android.widget.ListView;  
      19. import android.widget.TextView;  
      20.   
      21. public class TestAndroidActivity extends Activity {  
      22.     /** Called when the activity is first created. */  
      23.       
      24.     private Button button,add;  
      25.     private TextView text;  
      26.     private ListView listview;  
      27.     public MyAdapter adapter;  
      28.     @Override  
      29.     public void onCreate(Bundle savedInstanceState) {  
      30.         super.onCreate(savedInstanceState);  
      31.         setContentView(R.layout.main);  
      32.         listview = (ListView) findViewById(R.id.listview);  
      33.         add = (Button) findViewById(R.id.add);  
      34.         adapter = new MyAdapter(this);  
      35.         listview.setAdapter(adapter);  
      36.           
      37.         add.setOnClickListener(new OnClickListener() {  
      38.             @Override  
      39.             public void onClick(View arg0) {  
      40.                 // TODO Auto-generated method stub  
      41.                 adapter.arr.add("");  
      42.                 adapter.notifyDataSetChanged();  
      43.             }  
      44.         });  
      45.     }  
      46.       
      47.       
      48.     private class MyAdapter extends BaseAdapter {  
      49.   
      50.         private Context context;  
      51.         private LayoutInflater inflater;  
      52.         public ArrayList<String> arr;  
      53.         public MyAdapter(Context context) {  
      54.             super();  
      55.             this.context = context;  
      56.             inflater = LayoutInflater.from(context);  
      57.             arr = new ArrayList<String>();  
      58.             for(int i=0;i<3;i++){    //listview初始化3个子项  
      59.                 arr.add("");  
      60.             }  
      61.         }  
      62.         @Override  
      63.         public int getCount() {  
      64.             // TODO Auto-generated method stub  
      65.             return arr.size();  
      66.         }  
      67.         @Override  
      68.         public Object getItem(int arg0) {  
      69.             // TODO Auto-generated method stub  
      70.             return arg0;  
      71.         }  
      72.         @Override  
      73.         public long getItemId(int arg0) {  
      74.             // TODO Auto-generated method stub  
      75.             return arg0;  
      76.         }  
      77.         @Override  
      78.         public View getView(final int position, View view, ViewGroup arg2) {  
      79.             // TODO Auto-generated method stub  
      80.             if(view == null){  
      81.                 view = inflater.inflate(R.layout.list_item, null);  
      82.             }  
      83.             final EditText edit = (EditText) view.findViewById(R.id.edit);  
      84.             edit.setText(arr.get(position));    //在重构adapter的时候不至于数据错乱  
      85.             Button del = (Button) view.findViewById(R.id.del);  
      86.             edit.setOnFocusChangeListener(new OnFocusChangeListener() {  
      87.                 @Override  
      88.                 public void onFocusChange(View v, boolean hasFocus) {  
      89.                     // TODO Auto-generated method stub  
      90.                     if(arr.size()>0){  
      91.                         arr.set(position, edit.getText().toString());  
      92.                     }  
      93.                 }  
      94.             });  
      95.             del.setOnClickListener(new OnClickListener() {  
      96.                 @Override  
      97.                 public void onClick(View arg0) {  
      98.                     // TODO Auto-generated method stub  
      99.                     //从集合中删除所删除项的EditText的内容  
      100.                     arr.remove(position);  
      101.                     adapter.notifyDataSetChanged();  
      102.                 }  
      103.             });  
      104.             return view;  
      105.         }  
      106.     }  
      107. }  

    http://blog.csdn.net/centralperk/article/details/7446726

  • 相关阅读:
    如何降低微服务测试成本?我的经验之谈
    Serverless 在 SaaS 领域的最佳实践
    技术干货 | “选图预览并上传”的场景如何解?全网最全方案汇总来了
    SRE技术保障平台-盯屏中心TAC: 混合云一站式告警运维平台
    DTCC 2020 | 阿里云王涛:阿里巴巴电商数据库上云实践
    中值滤波算法 C
    python logger.debug_python中的logger模块讲解
    唯一值
    接触jeecgBoot低代码开发
    php数字操作
  • 原文地址:https://www.cnblogs.com/daishuguang/p/4025543.html
Copyright © 2020-2023  润新知