android studio listview长按删除

activity_main.xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="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" />
</android.support.constraint.ConstraintLayout>

item.xml 这是listview中的项

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="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" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

strings.xml一些字符串资源,后来嫌麻烦,直接硬编码了

<resources>
    <string name="app_name">deletetry1</string>
    <string name="grade">计算机151</string>
    <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>
</resources>

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<Map<String, Object>> mlistItems;
    Map<String, Object> mmap;
    //定义一个simpleAdapter,供列表项使用
    SimpleAdapter mSimpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(android.R.id.list);
        //为所有的listview的item注册contextMenu
        this.registerForContextMenu(listview);

        mlistItems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < mStudInfo.length; i++) {
            mmap = new HashMap<String, Object>();
            mmap.put("grade", mStudInfo[i].getGrade());
            mmap.put("studentId", mStudInfo[i].getStudentId());
            mmap.put("name", mStudInfo[i].getName());
            mlistItems.add(mmap);
        }
        mSimpleAdapter = new SimpleAdapter(this, mlistItems, R.layout.item, new String[]{"grade", "studentId", "name"}, new int[]{R.id.grade, R.id.studentId, R.id.name});
        listview.setAdapter(mSimpleAdapter);

        //添加对象
        mAddButton = (Button) findViewById(R.id.button);
        grade = (EditText) findViewById(R.id.editText3);
        studentid = (EditText) findViewById(R.id.editText4);
        name = (EditText) findViewById(R.id.editText5);
        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mmap = new HashMap<String, Object>();
                mmap.put("grade", grade.getText());
                mmap.put("studentId", studentid.getText());
                mmap.put("name", name.getText());
                mlistItems.add(mmap);
                mSimpleAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("选择操作");
        menu.add(0, 1, Menu.NONE, "删除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case 1:
                //删除
                int pos = (int) listview.getAdapter().getItemId(menuInfo.position);
                if (mlistItems.remove(pos) != null) {
                    System.out.println("success");
                } else {
                    System.out.println("failed");
                }
                mSimpleAdapter.notifyDataSetChanged();
                Toast.makeText(getBaseContext(), "删除此项", Toast.LENGTH_SHORT).show();
                break;
            default:
                //标记
                return super.onContextItemSelected(item);
        }
        return true;
    }

}

效果图如上,这个例子使用

onCreateContextMenu为每个listview的项创建了一个菜单,然后使用
onContextItemSelected来响应当菜单被选中时的操作
posted @ 2018-06-23 21:22  diamondDemand  阅读(1270)  评论(0编辑  收藏  举报