activity_main.xml 的代码
"1.0" encoding="utf-8"?>"http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView5" android:layout_width="45dp" android:layout_height="17dp" android:layout_marginLeft="4dp" android:layout_marginStart="4dp" android:layout_marginTop="16dp" android:text="班级:" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView6" android:layout_width="47dp" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginStart="4dp" android:layout_marginTop="29dp" android:text="学号:" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView5" /> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_marginStart="12dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintStart_toEndOf="@+id/textView5" tools:layout_editor_absoluteY="0dp" /> <EditText android:id="@+id/editText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_marginStart="12dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintStart_toEndOf="@+id/textView6" app:layout_constraintTop_toBottomOf="@+id/editText3" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginStart="4dp" android:layout_marginTop="28dp" android:text="姓名:" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView6" /> <EditText android:id="@+id/editText5" android:layout_width="wrap_content" android:layout_height="45dp" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintStart_toEndOf="@+id/textView7" app:layout_constraintTop_toBottomOf="@+id/editText4" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginStart="4dp" android:layout_marginTop="8dp" android:text="添加" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText5" /> <ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="313dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" app:layout_constraintVertical_bias="0.0" />
item.xml 这是listview中的项
"1.0" encoding="utf-8"?>"http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/grade" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text= "班级" android:textSize="20sp" android:textColor="#0b0a0b" tools:layout_editor_absoluteX="46dp" android:paddingLeft="20dp" tools:layout_editor_absoluteY="0dp" /> <TextView android:id="@+id/studentId" android:layout_width="wrap_content" android:layout_height="27dp" android:layout_weight="1" android:text="学号" android:textSize="20sp" android:textColor="#0b0a0b" tools:layout_editor_absoluteX="89dp" android:paddingLeft="20dp" tools:layout_editor_absoluteY="0dp" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="27dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_weight="1" android:text="姓名" android:textColor="#0b0a0b" android:paddingLeft="20dp" android:textSize="20sp" />
strings.xml一些字符串资源,后来嫌麻烦,直接硬编码了
<string name="app_name">deletetry1string> <string name="grade">计算机151string> <string name="score">成绩string> <string name="banji">班级string> <string name="stid">学号string> <string name="name1">王菲string> <string name="name2">张宇string> <string name="name3">张学友string> <string name="name4">刘德华string>
information类,这个类用来管理学生信息
package com.example.cie.deletetry1; public class Information { private String mGrade; private int mStudentId; private String mName; public Information(String grade, int studentId, String mName) { mGrade = grade; mStudentId = studentId; this.mName = mName; } public String getGrade() { return mGrade; } public void setGrade(String grade) { mGrade = grade; } public int getStudentId() { return mStudentId; } public void setStudentId(int studentId) { mStudentId = studentId; } public String getName() { return mName; } public void setName(String mName) { this.mName = mName; } }
mainActivity.java
package com.example.cie.deletetry1; import android.app.ListActivity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.*; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends ListActivity { private Button mAddButton; private EditText grade, studentid, name; private Information[] mStudInfo = new Information[]{ new Information("计算机151", 2014012543, "张宇"), new Information("计算机151", 2014012544, "刘德华"), new Information("计算机151", 2014012545, "张学友"), }; private ListView listview; List
效果图如上,这个例子使用
onCreateContextMenu为每个listview的项创建了一个菜单,然后使用
onContextItemSelected来响应当菜单被选中时的操作