安卓--ListView

仓廪实则知礼节,衣食足则知荣辱。这篇文章主要讲述安卓--ListView相关的知识,希望能为你提供帮助。
【安卓--ListView】实验目的:
学习使用ListView
实验要求:
实现一个列表,其中显示班级学号姓名,提供添加功能,如需要删去某一项,长按该项,通过弹出菜单显示删除功能。
 

安卓--ListView

文章图片
安卓--ListView

文章图片
1 package com.flyuz.app3; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.MenuItem ; 7 import android.widget.Button; 8 import android.widget.ListView; 9 import android.widget.ArrayAdapter; 10 import android.widget.EditText; 11 import android.widget.PopupMenu; 12 import android.widget.AdapterView; 13 import java.util.List; 14 import java.util.ArrayList; 15 16 public class MainActivity extends AppCompatActivity { 17private Button bt_add; 18private ListView lv; 19 20@Override 21protected void onCreate(Bundle savedInstanceState) { 22super.onCreate(savedInstanceState); 23setContentView(R.layout.activity_main); 24init(); 25} 26 27private void init() { 28bt_add = (Button) findViewById(R.id.bt_add); 29lv = (ListView) findViewById(R.id.lv); 30List< String> list = new ArrayList< String> (); 31list.add("班级" + "" + "学号" + "" + "姓名"); 32list.add("se1" + "" + "2016012884" + "" + "张三"); 33ArrayAdapter< String> adapter = new ArrayAdapter< String> (this, android.R.layout.simple_list_item_1, list); 34lv.setAdapter(adapter); 35 36//设置事件 37bt_add.setOnClickListener(new View.OnClickListener() { 38public void onClick(View v) { 39EditText et_class = (EditText) findViewById(R.id.et_class); 40EditText et_number = (EditText) findViewById(R.id.et_number); 41EditText et_name = (EditText) findViewById(R.id.et_name); 42String str_class = et_class.getText().toString(); 43String str_number = et_number.getText().toString(); 44String str_name = et_name.getText().toString(); 45 46ArrayAdapter add_adp = (ArrayAdapter) lv.getAdapter(); 47add_adp.add(str_class + "" + str_number + "" + str_name); 48} 49}); 50lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 51@Override 52public boolean onItemLongClick(AdapterView< ?> parent, View view, final int position, long id) { 53if (id > 0) { 54PopupMenu popup = new PopupMenu(MainActivity.this, view); 55popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu()); 56popup.show(); 57popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 58@Override 59public boolean onMenuItemClick(MenuItem item) { 60switch (item.getItemId()) { 61case R.id.menu_del: 62ArrayAdapter temp_adp = (ArrayAdapter) lv.getAdapter(); 63temp_adp.remove(temp_adp.getItem(position)); 64return true; 65default: 66return false; 67} 68} 69}); 70} 71return true; 72} 73}); 74} 75 }

MainActivity
安卓--ListView

文章图片
安卓--ListView

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7android:orientation="vertical" 8tools:context="com.flyuz.app3.MainActivity"> 9 10< LinearLayout 11android:layout_width="match_parent" 12android:layout_height="200dp" 13android:gravity="center" 14android:orientation="vertical"> 15 16< LinearLayout 17android:layout_width="match_parent" 18android:layout_height="50dp" 19android:gravity="center" 20android:orientation="horizontal"> 21 22< TextView 23android:layout_width="50dp" 24android:layout_height="40dp" 25android:gravity="center" 26android:text="班级:" /> 27 28< EditText 29android:id="@+id/et_class" 30android:layout_width="150dp" 31android:layout_height="40dp" /> 32< /LinearLayout> 33 34< LinearLayout 35android:layout_width="match_parent" 36android:layout_height="50dp" 37android:gravity="center" 38android:orientation="horizontal"> 39 40< TextView 41android:layout_width="50dp" 42android:layout_height="40dp" 43android:gravity="center" 44android:text="学号:" /> 45 46< EditText 47android:id="@+id/et_number" 48android:layout_width="150dp" 49android:layout_height="40dp" /> 50< /LinearLayout> 51 52< LinearLayout 53android:layout_width="match_parent" 54android:layout_height="50dp" 55android:gravity="center" 56android:orientation="horizontal"> 57 58< TextView 59android:layout_width="50dp" 60android:layout_height="40dp" 61android:gravity="center" 62android:text="姓名:" /> 63 64< EditText 65android:id="@+id/et_name" 66android:layout_width="150dp" 67android:layout_height="40dp" /> 68< /LinearLayout> 69 70< Button 71android:id="@+id/bt_add" 72android:layout_width="180dp" 73android:layout_height="40dp" 74android:text="添加" /> 75 76< /LinearLayout> 77< ListView 78android:id="@+id/lv" 79android:layout_width="match_parent" 80android:layout_height="wrap_content" 81android:longClickable="true" /> 82 83 < /LinearLayout>

layoutactivity_main.xml
安卓--ListView

文章图片
安卓--ListView

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < menu xmlns:android="http://schemas.android.com/apk/res/android"> 3< item 4android:id="@+id/menu_del" 5android:title="删除"> 6< /item> 7 < /menu>

menumenu.xml 

    推荐阅读