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 }