Dynamic addition and deletion of data within the Android ListView

Source: Internet
Author: User

Main.xml file:

[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="Add"
  22. />
  23. </LinearLayout>
  24. </LinearLayout>


Listview_item.xml file:

[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. <edittext
  10. android:id="@+id/edit"
  11. Android:layout_width="200DP"
  12. android:layout_height="Wrap_content"
  13. />
  14. <button
  15. android:id="@+id/del"
  16. Android:layout_width="Wrap_content"
  17. android:layout_height="Wrap_content"
  18. android:text="Delete"
  19. />
  20. </LinearLayout>


Mainactivity. java

[Java]View Plaincopy
    1. Package com.yyy.testandroid;
    2. Import java.util.ArrayList;
    3. Import android.app.Activity;
    4. Import Android.content.Context;
    5. Import Android.os.Bundle;
    6. Import Android.view.LayoutInflater;
    7. Import Android.view.View;
    8. Import Android.view.View.OnClickListener;
    9. Import Android.view.View.OnFocusChangeListener;
    10. Import Android.view.ViewGroup;
    11. Import Android.widget.BaseAdapter;
    12. Import Android.widget.Button;
    13. Import Android.widget.EditText;
    14. Import Android.widget.ListView;
    15. Import Android.widget.TextView;
    16. Public class Testandroidactivity extends Activity {
    17. /** Called when the activity is first created. * /
    18. private Button Button,add;
    19. private TextView text;
    20. Private ListView ListView;
    21. Public Myadapter Adapter;
    22. @Override
    23. public void OnCreate (Bundle savedinstancestate) {
    24. super.oncreate (savedinstancestate);
    25. Setcontentview (R.layout.main);
    26. ListView = (ListView) Findviewbyid (R.id.listview);
    27. Add = (Button) Findviewbyid (R.id.add);
    28. adapter = New Myadapter (this);
    29. Listview.setadapter (adapter);
    30. Add.setonclicklistener (new Onclicklistener () {
    31. @Override
    32. public void OnClick (View arg0) {
    33. //TODO auto-generated method stub
    34. Adapter.arr.add ("");
    35. Adapter.notifydatasetchanged ();
    36. }
    37. });
    38. }
    39. private class Myadapter extends Baseadapter {
    40. private context context;
    41. private Layoutinflater Inflater;
    42. Public arraylist<string> arr;
    43. Public Myadapter (context context) {
    44. super ();
    45. This.context = context;
    46. Inflater = Layoutinflater.from (context);
    47. arr = new arraylist<string> ();
    48. For (int i=0;i<3;i++) { //listview Initialize 3 subkeys
    49. Arr.add ("");
    50. }
    51. }
    52. @Override
    53. public int GetCount () {
    54. //TODO auto-generated method stub
    55. return arr.size ();
    56. }
    57. @Override
    58. Public Object getItem (int arg0) {
    59. //TODO auto-generated method stub
    60. return arg0;
    61. }
    62. @Override
    63. public long getitemid (int arg0) {
    64. //TODO auto-generated method stub
    65. return arg0;
    66. }
    67. @Override
    68. Public View GetView (final int position, view view, ViewGroup arg2) {
    69. //TODO auto-generated method stub
    70. if (view = = null) {
    71. View = Inflater.inflate (R.layout.list_item, null);
    72. }
    73. final EditText edit = (EditText) View.findviewbyid (R.id.edit);
    74. Edit.settext (Arr.get (position)); //No data confusion when refactoring adapter
    75. Button del = (button) View.findviewbyid (R.id.del);
    76. Edit.setonfocuschangelistener (new Onfocuschangelistener () {
    77. @Override
    78. public void Onfocuschange (View V, boolean hasfocus) {
    79. //TODO auto-generated method stub
    80. if (arr.size () >0) {
    81. Arr.set (position, Edit.gettext (). toString ());
    82. }
    83. }
    84. });
    85. Del.setonclicklistener (new Onclicklistener () {
    86. @Override
    87. public void OnClick (View arg0) {
    88. //TODO auto-generated method stub
    89. //Delete the contents of the EditText of the deleted item from the collection
    90. Arr.remove (position);
    91. Adapter.notifydatasetchanged ();
    92. }
    93. });
    94. return view;
    95. }
    96. }
    97. }

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

Dynamic addition and deletion of data within the Android ListView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.