Listview实现学生信息增删
实验目的:
学习使用ListView
实验要求:
1. 实现一个列表,其中显示班级学号姓名,提供添加功能,如需要删去某一项,长按该项,通过弹出菜单显示删除功能。
(1)Activity_main.xml文件:
1.<?xml version="1.0" encoding="utf-8"?> 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. xmlns:tools="http://schemas.android.com/tools" 4. android:layout_width="match_parent" 5. android:layout_height="match_parent" 6. android:orientation="vertical" 7. tools:context="com.example.administrator.myapplicationthree.MainActivity"> 8. 9. <LinearLayout 10. android:layout_width="370dp" 11. android:layout_height="200dp" 12. android:layout_marginBottom="8dp" 13. android:orientation="vertical" 14. tools:layout_editor_absoluteX="8dp" 15. tools:layout_editor_ansoluteY="0dp"> 16. 17. <LinearLayout 18. android:layout_width="370dp" 19. android:layout_height="50dp" 20. android:orientation="horizontal"> 21. 22. <TextView 23. android:layout_width="50dp" 24. android:layout_height="28dp" 25. android:text="班级"/> 26. 27. <EditText 28. android:id="@+id/et_c" 29. android:layout_width="150dp" 30. android:layout_height="40dp" /> 31. </LinearLayout> 32. 33. <LinearLayout 34. android:layout_width="370dp" 35. android:layout_height="50dp" 36. android:orientation="horizontal"> 37. 38. <TextView 39. android:layout_width="50dp" 40. android:layout_height="28dp" 41. android:text="学号"/> 42. 43. <EditText 44. android:id="@+id/et_a" 45. android:layout_width="150dp" 46. android:layout_height="40dp" /> 47. </LinearLayout> 48. 49. <LinearLayout 50. android:layout_width="370dp" 51. android:layout_height="50dp" 52. android:orientation="horizontal"> 53. 54. <TextView 55. android:layout_width="50dp" 56. android:layout_height="28dp" 57. android:text="姓名"/> 58. 59. <EditText 60. android:id="@+id/et_n" 61. android:layout_width="150dp" 62. android:layout_height="40dp" /> 63. </LinearLayout> 64. 65. <Button 66. android:id="@+id/btn1" 67. android:layout_width="80dp" 68. android:layout_height="40dp" 69. android:text="添加"/> 70. </LinearLayout> 71. 72. <ListView 73. android:id="@+id/lv1" 74. android:layout_width="match_parent" 75. android:layout_height="match_parent" 76. android:longClickable="true"> 77. 78. </ListView> </LinearLayout>
(2)smenul.xml文件,在res下面建一个menu目录,然后在该目录下建立一个文件smenu1
1.<?xml version="1.0" encoding="utf-8"?> 2.<menu xmlns:app="http://schemas.android.com/apk/res-auto" 3. xmlns:android="http://schemas.android.com/apk/res/android" 4. android:layout_width="200dp" 5. android:layout_height="50dp" 6. > 7. <item 8. android:id="@+id/menu1" 9. android:orderInCategory="100" 10. android:title="删除"> 11. 12. </item> 13. 14.</menu>
(3)MainActivity.java文件:
1.package com.example.administrator.myapplicationthree; 2. 3.import android.support.v7.app.AppCompatActivity; 4.import android.os.Bundle; 5.import android.app.Activity; 6.import android.view.MenuItem; 7.import android.view.View; 8.import android.widget.Adapter; 9.import android.widget.AdapterView; 10.import android.widget.ArrayAdapter; 11.import android.widget.Button; 12.import android.widget.EditText; 13.import android.widget.ListView; 14.import android.widget.PopupMenu; 15.import java.util.ArrayList; 16.import java.util.List; 17.import android.view.Menu; 18. 19. 20.public class MainActivity extends Activity { 21. 22. @Override 23. protected void onCreate(Bundle savedInstanceState) { 24. super.onCreate(savedInstanceState); 25. setContentView(R.layout.activity_main); 26. final Button btn1=(Button)findViewById(R.id.btn1); 27. final ListView lv1=(ListView)findViewById(R.id.lv1); 28. 29. List<String> list=new ArrayList<String>(); 30. list.add(" 班级 "+" "+"学号"+" "+"姓名"); 31. ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); 32. lv1.setAdapter(adapter); 33. 34. btn1.setOnClickListener(new View.OnClickListener(){ 35. @Override 36. public void onClick(View v) 37. { 38. EditText et_c,et_a,et_n; 39. et_c=(EditText)findViewById(R.id.et_c); 40. et_a=(EditText)findViewById(R.id.et_a); 41. et_n=(EditText)findViewById(R.id.et_n); 42. ArrayAdapter temp_adp=(ArrayAdapter) lv1.getAdapter(); 43. temp_adp.add(et_c.getText().toString()+" "+et_a.getText().toString()+" "+et_n.getText().toString()); 44. 45. } 46. }); 47. 48. lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){ 49. @Override 50. 51. public boolean onItemLongClick(AdapterView<?> parent,View view,final int position, long id){ 52. if(id > 0) 53. { 54. PopupMenu popup = new PopupMenu(MainActivity.this,view); 55. popup.getMenuInflater().inflate(R.menu.smenu1,popup.getMenu()); 56. popup.show(); 57. popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){ 58. @Override 59. public boolean onMenuItemClick(MenuItem item){ 60. switch (item.getItemId()){ 61. case R.id.menu1: 62. ArrayAdapter temp_adp = (ArrayAdapter) lv1.getAdapter(); 63. temp_adp.remove(temp_adp.getItem(position)); 64. return true; 65. default: 66. return false; 67. } 68. } 69. 70. }); 71. } 72. return true; 73. 74. } 75. }); 76. } }
(4)AndroidManiFest.xml文件:
1.<?xml version="1.0" encoding="utf-8"?> 2.<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3. package="com.example.administrator.myapplicationthree"> 4. 5. <application 6. android:allowBackup="true" 7. android:icon="@mipmap/ic_launcher" 8. android:label="@string/app_name" 9. android:roundIcon="@mipmap/ic_launcher_round" 10. android:supportsRtl="true" 11. android:theme="@style/AppTheme"> 12. <activity android:name=".MainActivity"> 13. <intent-filter> 14. <action android:name="android.intent.action.MAIN" /> 15. 16. <category android:name="android.intent.category.LAUNCHER" /> 17. </intent-filter> 18. </activity> 19. </application> 20. 21.</manifest>