安卓--ListView
Posted feifei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓--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 { 17 private Button bt_add; 18 private ListView lv; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 init(); 25 } 26 27 private void init() { 28 bt_add = (Button) findViewById(R.id.bt_add); 29 lv = (ListView) findViewById(R.id.lv); 30 List<String> list = new ArrayList<String>(); 31 list.add("班级" + " " + "学号" + " " + "姓名"); 32 list.add("se1" + " " + "2016012884" + " " + "张三"); 33 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); 34 lv.setAdapter(adapter); 35 36 //设置事件 37 bt_add.setOnClickListener(new View.OnClickListener() { 38 public void onClick(View v) { 39 EditText et_class = (EditText) findViewById(R.id.et_class); 40 EditText et_number = (EditText) findViewById(R.id.et_number); 41 EditText et_name = (EditText) findViewById(R.id.et_name); 42 String str_class = et_class.getText().toString(); 43 String str_number = et_number.getText().toString(); 44 String str_name = et_name.getText().toString(); 45 46 ArrayAdapter add_adp = (ArrayAdapter) lv.getAdapter(); 47 add_adp.add(str_class + " " + str_number + " " + str_name); 48 } 49 }); 50 lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 51 @Override 52 public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { 53 if (id > 0) { 54 PopupMenu popup = new PopupMenu(MainActivity.this, view); 55 popup.getMenuInflater().inflate(R.menu.menu, 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.menu_del: 62 ArrayAdapter temp_adp = (ArrayAdapter) lv.getAdapter(); 63 temp_adp.remove(temp_adp.getItem(position)); 64 return true; 65 default: 66 return false; 67 } 68 } 69 }); 70 } 71 return true; 72 } 73 }); 74 } 75 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context="com.flyuz.app3.MainActivity"> 9 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="200dp" 13 android:gravity="center" 14 android:orientation="vertical"> 15 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="50dp" 19 android:gravity="center" 20 android:orientation="horizontal"> 21 22 <TextView 23 android:layout_width="50dp" 24 android:layout_height="40dp" 25 android:gravity="center" 26 android:text="班级:" /> 27 28 <EditText 29 android:id="@+id/et_class" 30 android:layout_width="150dp" 31 android:layout_height="40dp" /> 32 </LinearLayout> 33 34 <LinearLayout 35 android:layout_width="match_parent" 36 android:layout_height="50dp" 37 android:gravity="center" 38 android:orientation="horizontal"> 39 40 <TextView 41 android:layout_width="50dp" 42 android:layout_height="40dp" 43 android:gravity="center" 44 android:text="学号:" /> 45 46 <EditText 47 android:id="@+id/et_number" 48 android:layout_width="150dp" 49 android:layout_height="40dp" /> 50 </LinearLayout> 51 52 <LinearLayout 53 android:layout_width="match_parent" 54 android:layout_height="50dp" 55 android:gravity="center" 56 android:orientation="horizontal"> 57 58 <TextView 59 android:layout_width="50dp" 60 android:layout_height="40dp" 61 android:gravity="center" 62 android:text="姓名:" /> 63 64 <EditText 65 android:id="@+id/et_name" 66 android:layout_width="150dp" 67 android:layout_height="40dp" /> 68 </LinearLayout> 69 70 <Button 71 android:id="@+id/bt_add" 72 android:layout_width="180dp" 73 android:layout_height="40dp" 74 android:text="添加" /> 75 76 </LinearLayout> 77 <ListView 78 android:id="@+id/lv" 79 android:layout_width="match_parent" 80 android:layout_height="wrap_content" 81 android:longClickable="true" /> 82 83 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="http://schemas.android.com/apk/res/android"> 3 <item 4 android:id="@+id/menu_del" 5 android:title="删除"> 6 </item> 7 </menu>
以上是关于安卓--ListView的主要内容,如果未能解决你的问题,请参考以下文章