android 对listview数据的增删改查

来源:互联网 发布:中国大一统知乎 编辑:程序博客网 时间:2024/05/29 17:58


android 对listview数据的增删改查


android listview是常用的数据展示控件。其中对数据的增删改查操作也是常用的功能,比如重新获取了新的数据,或者添加,删除某条记录等等更新都需要对listview的数据进行重新的加载。listview更新数据只需要调用到notifyDataSetChanged()方法,就可以实现对listview数据的更新。如果涉及到线程,则需要在UI的线程更新。

本文针对listview常用的操作进行listview item的数据实行增删改查的操作,主要界面如下。

图-1 android 实现对listview数据的增删改查主要界面

本例只有一个UpdateListViewActivity类,相关的操作以及功能均在一个界面完成。对listview 数据的增删改查,实质就是对填充的数组(填充的数据)进行重新修改后,再次调用notifyDataSetChanged()方法进行更新。尽管如此,对数据使用起来还是比较生疏,下面的例子能加强对listview数据更新的理解。

UpdateListViewActivity.java类是程序的唯一的一个类,实现了对listview增删改查的操作,代码比较多,但理解并不比较复杂。

1.UpdateListViewActivity.java

view plaincopy to clipboardprint?
  1. package com.updatelistview.main;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.AlertDialog;  
  8. import android.content.Context;  
  9. import android.content.DialogInterface;  
  10. import android.os.Bundle;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.EditText;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.ListView;  
  18. import android.widget.SimpleAdapter;  
  19. import android.widget.Toast;  
  20.   
  21. public class UpdateListViewActivity extends Activity {  
  22.     // 控件  
  23.     private Button addBtn;  
  24.     private Button deleteBtn;  
  25.     private Button editBtn;  
  26.     private Button queryBtn;  
  27.     private ListView listview;  
  28.     // 数组  
  29.     private SimpleAdapter listItemAdapter;  
  30.     private ArrayList<hashmap<string, object="">> listItem = null;  
  31.   
  32.     /** Called when the activity is first created. */  
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main);  
  37.   
  38.         // 获取控件  
  39.         addBtn = (Button) findViewById(R.id.add_id);  
  40.         deleteBtn = (Button) findViewById(R.id.delete_id);  
  41.         editBtn = (Button) findViewById(R.id.edit_id);  
  42.         queryBtn = (Button) findViewById(R.id.query_id);  
  43.         listview = (ListView) findViewById(R.id.show_result);  
  44.   
  45.         // 初始化数据  
  46.         init();  
  47.   
  48.         // 设置控件事件监听  
  49.         addBtn.setOnClickListener(addClick);  
  50.         deleteBtn.setOnClickListener(deleteClick);  
  51.         editBtn.setOnClickListener(editClick);  
  52.         queryBtn.setOnClickListener(queryClick);  
  53.   
  54.     }  
  55.   
  56.     // 添加事件响应  
  57.     OnClickListener addClick = new OnClickListener() {  
  58.   
  59.         @Override  
  60.         public void onClick(View v) {  
  61.             // TODO Auto-generated method stub  
  62.   
  63.             // 加载输入框的布局文件  
  64.             LayoutInflater inflater = (LayoutInflater) UpdateListViewActivity.this  
  65.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  66.             final LinearLayout layout = (LinearLayout) inflater.inflate(  
  67.                     R.layout.input_add, null);  
  68.   
  69.             // 弹出的对话框  
  70.   
  71.             new AlertDialog.Builder(UpdateListViewActivity.this)  
  72.                     /* 弹出窗口的最上头文字 */  
  73.                     .setTitle("添加一条数据")  
  74.                     /* 设置弹出窗口的图式 */  
  75.                     .setIcon(android.R.drawable.ic_dialog_info)  
  76.                     /* 设置弹出窗口的信息 */  
  77.                     .setMessage("请输入添加的内容")  
  78.                     .setView(layout)  
  79.                     .setPositiveButton("确定",  
  80.                             new DialogInterface.OnClickListener() {  
  81.                                 public void onClick(  
  82.                                         DialogInterface dialoginterface, int i) {  
  83.   
  84.                                     EditText inputStringr = (EditText) layout  
  85.                                             .findViewById(R.id.input_add_string);  
  86.   
  87.                                     String str = inputStringr.getText()  
  88.                                             .toString();  
  89.   
  90.                                     if (str == null || str.equals("")) {  
  91.   
  92.                                         Toast.makeText(getApplicationContext(),  
  93.                                                 "添加的内容不能为空", Toast.LENGTH_SHORT)  
  94.                                                 .show();  
  95.                                     } else {  
  96.                                         HashMap<string, object=""> map = new HashMap<string, object="">();  
  97.                                         map.put("viewspot", str);  
  98.                                         map.put("add", R.drawable.right);  
  99.                                         listItem.add(0, map);  
  100.                                         // 如果在前面添加一条数据添加  
  101.                                         // listItem.add(map);  
  102.                                         listItemAdapter.notifyDataSetChanged();  
  103.                                         Toast.makeText(  
  104.                                                 UpdateListViewActivity.this,  
  105.                                                 "添加的一条数据为:" + str + "",  
  106.                                                 Toast.LENGTH_SHORT).show();  
  107.   
  108.                                     }  
  109.   
  110.                                 }  
  111.                             })  
  112.                     .setNegativeButton("取消",  
  113.                             new DialogInterface.OnClickListener() { /* 设置跳出窗口的返回事件 */  
  114.                                 public void onClick(  
  115.                                         DialogInterface dialoginterface, int i) {  
  116.                                     Toast.makeText(UpdateListViewActivity.this,  
  117.                                             "取消了删除数据", Toast.LENGTH_SHORT)  
  118.                                             .show();  
  119.   
  120.                                 }  
  121.                             }).show();  
  122.   
  123.         }  
  124.     };  
  125.   
  126.     // 删除事件响应  
  127.     OnClickListener deleteClick = new OnClickListener() {  
  128.   
  129.         @Override  
  130.         public void onClick(View v) {  
  131.             // TODO Auto-generated method stub  
  132.   
  133.             /** 
  134.              * listItem.clear();清空所有数据 
  135.              *  
  136.              * */  
  137.   
  138.             /* 
  139.              * listItem.clear();  
  140.              * listItemAdapter.notifyDataSetChanged(); 
  141.              */  
  142.   
  143.             // 加载输入框的布局文件  
  144.             LayoutInflater inflater = (LayoutInflater) UpdateListViewActivity.this  
  145.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  146.             final LinearLayout layout = (LinearLayout) inflater.inflate(  
  147.                     R.layout.input_delete, null);  
  148.   
  149.             // 弹出的对话框  
  150.   
  151.             new AlertDialog.Builder(UpdateListViewActivity.this)  
  152.                     /* 弹出窗口的最上头文字 */  
  153.                     .setTitle("删除一条数据")  
  154.                     /* 设置弹出窗口的图式 */  
  155.                     .setIcon(android.R.drawable.ic_dialog_info)  
  156.                     /* 设置弹出窗口的信息 */  
  157.                     .setMessage("请输入删除的索引")  
  158.                     .setView(layout)  
  159.                     .setPositiveButton("确定",  
  160.                             new DialogInterface.OnClickListener() {  
  161.                                 public void onClick(  
  162.                                         DialogInterface dialoginterface, int i) {  
  163.   
  164.                                     EditText inputNumber = (EditText) layout  
  165.                                             .findViewById(R.id.input_delete_number);  
  166.   
  167.                                     String str = inputNumber.getText()  
  168.                                             .toString();  
  169.   
  170.                                     if (str == null || str.equals("")) {  
  171.   
  172.                                         Toast.makeText(getApplicationContext(),  
  173.                                                 "请输入一个数字", Toast.LENGTH_SHORT)  
  174.                                                 .show();  
  175.                                     } else {  
  176.                                         int number = Integer.valueOf(str);  
  177.   
  178.                                         int size = listItem.size();  
  179.   
  180.                                         // 判断数字是否超出数组索引范围  
  181.                                         if (number >= size) {  
  182.                                             Toast.makeText(  
  183.                                                     getApplicationContext(),  
  184.                                                     "没有找到删除的数据索引",  
  185.                                                     Toast.LENGTH_SHORT).show();  
  186.   
  187.                                         } else {  
  188.   
  189.                                             String value = listItem.get(number)  
  190.                                                     .toString();  
  191.                                             listItem.remove(number);  
  192.                                             listItemAdapter  
  193.                                                     .notifyDataSetChanged();  
  194.                                             Toast.makeText(  
  195.                                                     UpdateListViewActivity.this,  
  196.                                                     "删除的数据为:" + value + "",  
  197.                                                     Toast.LENGTH_SHORT).show();  
  198.   
  199.                                         }  
  200.                                     }  
  201.   
  202.                                 }  
  203.                             })  
  204.                     .setNegativeButton("取消",  
  205.                             new DialogInterface.OnClickListener() { /* 设置跳出窗口的返回事件 */  
  206.                                 public void onClick(  
  207.                                         DialogInterface dialoginterface, int i) {  
  208.                                     Toast.makeText(UpdateListViewActivity.this,  
  209.                                             "取消了删除数据", Toast.LENGTH_SHORT)  
  210.                                             .show();  
  211.   
  212.                                 }  
  213.                             }).show();  
  214.   
  215.         }  
  216.     };  
  217.     // 修改事件响应  
  218.     OnClickListener editClick = new OnClickListener() {  
  219.   
  220.         @Override  
  221.         public void onClick(View v) {  
  222.             // TODO Auto-generated method stub  
  223.             // 加载输入框的布局文件  
  224.             LayoutInflater inflater = (LayoutInflater) UpdateListViewActivity.this  
  225.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  226.             final LinearLayout layout = (LinearLayout) inflater.inflate(  
  227.                     R.layout.input_edit, null);  
  228.   
  229.             // 弹出的对话框  
  230.   
  231.             new AlertDialog.Builder(UpdateListViewActivity.this)  
  232.                     /* 弹出窗口的最上头文字 */  
  233.                     .setTitle("修改一条数据")  
  234.                     /* 设置弹出窗口的图式 */  
  235.                     .setIcon(android.R.drawable.ic_dialog_info)  
  236.                     /* 设置弹出窗口的信息 */  
  237.                     .setMessage("请输入修改的索引及内容")  
  238.                     .setView(layout)  
  239.                     .setPositiveButton("确定",  
  240.                             new DialogInterface.OnClickListener() {  
  241.                                 public void onClick(  
  242.                                         DialogInterface dialoginterface, int i) {  
  243.   
  244.                                     EditText inputEditNumber = (EditText) layout  
  245.                                             .findViewById(R.id.input_edit_number);  
  246.   
  247.                                     String numberStr = inputEditNumber  
  248.                                             .getText().toString();  
  249.   
  250.                                     EditText inputEditString = (EditText) layout  
  251.                                             .findViewById(R.id.input_edit_string);  
  252.   
  253.                                     String editStr = inputEditString.getText()  
  254.                                             .toString();  
  255.   
  256.                                     if (numberStr == null  
  257.                                             || numberStr.equals("")) {  
  258.   
  259.                                         Toast.makeText(getApplicationContext(),  
  260.                                                 "请输入要修改的索引", Toast.LENGTH_SHORT)  
  261.                                                 .show();  
  262.                                     } else {  
  263.                                         int number = Integer.valueOf(numberStr);  
  264.   
  265.                                         int size = listItem.size();  
  266.   
  267.                                         // 判断数字是否超出数组索引范围  
  268.                                         if (number >= size) {  
  269.                                             Toast.makeText(  
  270.                                                     getApplicationContext(),  
  271.                                                     "没有找到修改的数据索引",  
  272.                                                     Toast.LENGTH_SHORT).show();  
  273.   
  274.                                         } else {  
  275.   
  276.                                             HashMap<string, object=""> map = new HashMap<string, object="">();  
  277.                                             map.put("viewspot", editStr);  
  278.                                             map.put("add", R.drawable.right);  
  279.   
  280.                                             listItem.set(number, map);  
  281.                                             listItemAdapter  
  282.                                                     .notifyDataSetChanged();  
  283.   
  284.                                             Toast.makeText(  
  285.                                                     UpdateListViewActivity.this,  
  286.                                                     "数据修改为:" + editStr + "",  
  287.                                                     Toast.LENGTH_SHORT).show();  
  288.   
  289.                                         }  
  290.                                     }  
  291.   
  292.                                 }  
  293.                             })  
  294.                     .setNegativeButton("取消",  
  295.                             new DialogInterface.OnClickListener() { /* 设置跳出窗口的返回事件 */  
  296.                                 public void onClick(  
  297.                                         DialogInterface dialoginterface, int i) {  
  298.                                     Toast.makeText(UpdateListViewActivity.this,  
  299.                                             "取消了修改数据", Toast.LENGTH_SHORT)  
  300.                                             .show();  
  301.   
  302.                                 }  
  303.                             }).show();  
  304.   
  305.         }  
  306.     };  
  307.   
  308.     // 查询事件响应  
  309.     OnClickListener queryClick = new OnClickListener() {  
  310.   
  311.         @Override  
  312.         public void onClick(View v) {  
  313.             // TODO Auto-generated method stub  
  314.             // 查询数据  
  315.             listItemAdapter.notifyDataSetChanged();  
  316.         }  
  317.     };  
  318.   
  319.     // 初始化数据  
  320.     private void init() {  
  321.   
  322.         listItem = new ArrayList<hashmap<string, object="">>();  
  323.         for (int i = 0; i < 12; i++) {  
  324.             HashMap<string, object=""> map = new HashMap<string, object="">();  
  325.             map.put("viewspot""北京故宫,颐和园" + i);  
  326.             map.put("add", R.drawable.right);  
  327.             listItem.add(map);  
  328.         }  
  329.         listItemAdapter = new SimpleAdapter(getApplicationContext(), listItem,// 数据源  
  330.                 R.layout.items, new String[] { "viewspot""add" }, new int[] {  
  331.                         R.id.viewspot, R.id.add });  
  332.         listview.setAdapter(listItemAdapter);  
  333.   
  334.     }  
  335.   
  336. }  
  337. </string,></string,></hashmap<string,></string,></string,></string,></string,></hashmap<string,>  

main.xml是主要的布局界面,也是UpdateListViewActivity加载的布局文件。

2.main.xml(主要界面)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:orientation="vertical"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="wrap_content">  
  6. <Button android:layout_width="fill_parent" android:text="添加数据" android:layout_height="wrap_content" android:id="@+id/add_id"></Button>  
  7. <Button android:layout_width="fill_parent" android:text="删除数据" android:layout_height="wrap_content" android:id="@+id/delete_id"></Button>  
  8. <Button android:layout_width="fill_parent" android:text="修改数据" android:layout_height="wrap_content" android:id="@+id/edit_id"></Button>  
  9. <Button android:layout_width="fill_parent" android:text="查询数据" android:layout_height="wrap_content" android:id="@+id/query_id"></Button>  
  10.   
  11. <ListView android:layout_width="fill_parent"  
  12. android:layout_height="wrap_content"  
  13. android:id="@+id/show_result"  
  14. >  
  15. </ListView>  
  16. </LinearLayout>  

items.xml是listview加载的数据选项布局文件

3.items.xml(listview item 内容填充布局)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="wrap_content">  
  6. <TextView android:layout_width="fill_parent"  
  7. android:layout_height="wrap_content"  
  8. android:id="@+id/viewspot"  
  9. android:padding="6dip"  
  10. android:layout_weight="1"  
  11. android:textColor="#cceecc">  
  12. </TextView>  
  13.   
  14. <ImageView android:layout_width="100dip"  
  15. android:layout_height="wrap_content"  
  16. android:id="@+id/add"  
  17. android:padding="10dip"  
  18. android:src="@drawable/right"></ImageView>  
  19.   
  20. </LinearLayout>  

input_add.xml是点击“添加数据”按钮时弹出AlertDialog对话框时加载的输入框布局文件

4.input_add.xml(弹出的添加界面)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent">  
  6. <EditText android:layout_width="fill_parent"  
  7. android:layout_height="wrap_content"  
  8. android:id="@+id/input_add_string"  
  9. android:singleLine="true"></EditText>  
  10. </LinearLayout>  

input_delete.xml是点击“删除数据”按钮时弹出AlertDialog对话框时加载的输入删除索引的布局文件

5.input_delete.xml(弹出的删除界面)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent">  
  6. <EditText android:layout_width="fill_parent"  
  7. android:layout_height="wrap_content"  
  8. android:id="@+id/input_delete_number"  
  9. android:singleLine="true"  
  10. android:digits="0123456789"></EditText>  
  11. </LinearLayout>  

input_edit.xml删除数据的布局文件

6.input_edit.xml(弹出的编辑界面)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent"  
  6. android:orientation="vertical">  
  7. <TextView android:layout_width="fill_parent"  
  8. android:layout_height="wrap_content"  
  9. android:text="输入要修改的索引"></TextView>  
  10. <EditText android:layout_width="fill_parent"  
  11. android:layout_height="wrap_content"  
  12. android:id="@+id/input_edit_number"  
  13. android:singleLine="true"  
  14. android:digits="0123456789"></EditText>  
  15. <TextView android:layout_width="fill_parent"  
  16. android:layout_height="wrap_content"  
  17. android:text="内容修改为"></TextView>  
  18. <EditText android:layout_width="fill_parent"  
  19. android:layout_height="wrap_content"  
  20. android:id="@+id/input_edit_string"  
  21. android:singleLine="true"></EditText>  
  22. </LinearLayout>  

编辑的界面如图-2

源码下载:UpdateListView.zip


0 0
原创粉丝点击