ListView长按删除信息

  1 package com.hanqi.testapp2;
  2 
  3 import android.app.AlertDialog;
  4 import android.os.Bundle;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.view.View;
  7 import android.view.ViewGroup;
  8 import android.widget.AdapterView;
  9 import android.widget.BaseAdapter;
 10 import android.widget.ImageView;
 11 import android.widget.ListView;
 12 import android.widget.TextView;
 13 
 14 import java.util.ArrayList;
 15 import java.util.List;
 16 
 17 public class Zuoye extends AppCompatActivity {
 18 
 19     ListView lv_3;
 20     List<MyClass> lm;
 21 
 22     @Override
 23     protected void onCreate(Bundle savedInstanceState) {
 24         super.onCreate(savedInstanceState);
 25         setContentView(R.layout.activity_zuoye);
 26 
 27         lv_3 = (ListView)findViewById(R.id.lv_3);
 28 
 29         lm = new ArrayList<>();
 30 
 31         MyClass myClass = new MyClass(R.drawable.f1,"美食1","美食1的介绍");
 32 
 33         lm.add(myClass);
 34         lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
 35         lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
 36         lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
 37         lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
 38         lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
 39         lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
 40         lm.add(new MyClass(R.drawable.f2, "美食2", "美食2的介绍"));
 41 
 42         final MyBaseAdapter myBaseAdapter = new MyBaseAdapter();
 43         lv_3.setAdapter(myBaseAdapter);
 44 
 45         lv_3.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
 46             @Override
 47             public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
 48 
 49                 lm.remove(position);
 50 
 51                 myBaseAdapter.notifyDataSetChanged();
 52 
 53                 return true;
 54             }
 55         });
 56 
 57     }
 58 
 59     class MyClass
 60     {
 61         private int img;
 62         private String name;
 63         private String content;
 64 
 65         public int getImg() {
 66             return img;
 67         }
 68 
 69         public void setImg(int img) {
 70             this.img = img;
 71         }
 72 
 73         public String getName() {
 74             return name;
 75         }
 76 
 77         public void setName(String name) {
 78             this.name = name;
 79         }
 80 
 81         public String getContent() {
 82             return content;
 83         }
 84 
 85         public void setContent(String content) {
 86             this.content = content;
 87         }
 88 
 89         public MyClass(int img, String name, String content)
 90         {
 91             this.img = img;
 92             this.name = name;
 93             this.content = content;
 94         }
 95     }
 96 
 97     class MyBaseAdapter extends BaseAdapter
 98     {
 99         @Override
100         public int getCount() {
101             return lm.size();
102         }
103 
104         @Override
105         public Object getItem(int position) {
106             return lm.get(position);
107         }
108 
109         @Override
110         public long getItemId(int position) {
111             return 0;
112         }
113 
114         @Override
115         public View getView(int position, View convertView, ViewGroup parent) {
116 
117             MyClass myClass = lm.get(position);
118 
119             View view = View.inflate(Zuoye.this, R.layout.simple_adapter, null);
120 
121             ImageView imageView = (ImageView)view.findViewById(R.id.iv_2);
122             imageView.setImageResource(myClass.getImg());
123 
124             TextView textView = (TextView)view.findViewById(R.id.tv_7);
125             textView.setText(myClass.getName());
126 
127             TextView textView1 = (TextView)view.findViewById(R.id.tv_8);
128             textView1.setText(myClass.getContent());
129 
130             return view;
131         }
132 
133     }
134 }
View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3      android:layout_width="match_parent"
 4     android:layout_height="wrap_content">
 5 
 6     <ImageView
 7         android:layout_width="70dp"
 8         android:layout_height="70dp"
 9         android:src="@drawable/f1"
10         android:id="@+id/iv_2"/>
11 
12     <LinearLayout
13         android:layout_width="0dp"
14         android:layout_height="match_parent"
15         android:orientation="vertical"
16         android:layout_weight="1"
17         android:layout_marginLeft="20dp"
18         android:gravity="center_vertical">
19 
20         <TextView
21             android:layout_width="match_parent"
22             android:layout_height="wrap_content"
23             android:text="名字=aaa"
24             android:id="@+id/tv_7"/>
25         <TextView
26             android:layout_width="match_parent"
27             android:layout_height="wrap_content"
28             android:text="内容=aaa"
29             android:id="@+id/tv_8"/>
30 
31 
32     </LinearLayout>
33 
34 </LinearLayout>
View Code
 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:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.Zuoye"
11     android:orientation="vertical">
12 
13 
14     <ListView
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:id="@+id/lv_3"></ListView>
18 
19 </LinearLayout>
View Code
原文地址:https://www.cnblogs.com/future-zhenzhen/p/5518810.html