1 package com.example.wang.testapp2;
2
3 import android.app.ActionBar;
4 import android.app.AlertDialog;
5 import android.content.DialogInterface;
6 import android.support.v7.app.AppCompatActivity;
7 import android.os.Bundle;
8 import android.util.Log;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.AbsListView;
12 import android.widget.AdapterView;
13 import android.widget.BaseAdapter;
14 import android.widget.EditText;
15 import android.widget.GridView;
16 import android.widget.ImageView;
17 import android.widget.ListView;
18 import android.widget.TextView;
19 import android.widget.Toast;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 public class TestActivity10 extends AppCompatActivity {
25
26 GridView gv_1;
27
28 List<MyClass> lm;
29 // List<Integer> liv;
30
31
32 @Override
33 protected void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35 setContentView(R.layout.activity_test10);
36
37 GridView gv_1=(GridView)findViewById(R.id.gv_1);
38
39 lm=new ArrayList<>();
40
41 final MyClass myClass=new MyClass(R.drawable.f1,"美食1");
42
43 lm.add(myClass);
44
45 lm.add(new MyClass(R.drawable.f2,"美食2"));
46 lm.add(new MyClass(R.drawable.f3,"美食3"));
47 lm.add(new MyClass(R.drawable.f4,"美食4"));
48 lm.add(new MyClass(R.drawable.f5,"美食5"));
49 lm.add(new MyClass(R.drawable.f6,"美食6"));
50 lm.add(new MyClass(R.drawable.f7,"美食7"));
51 lm.add(new MyClass(R.drawable.f8,"美食8"));
52
53
54 GridAdapter gridAdapter=new GridAdapter();
55
56 gv_1.setAdapter(gridAdapter);
57
58
59
60
61
62 // //1 数据 图片的id
63 // liv =new ArrayList<>();
64 // liv.add(R.drawable.f1);
65 // liv.add(R.drawable.f2);
66 // liv.add(R.drawable.f3);
67 // liv.add(R.drawable.f4);
68 // liv.add(R.drawable.f5);
69 // liv.add(R.drawable.f6);
70 // liv.add(R.drawable.f7);
71 // liv.add(R.drawable.f8);
72 // liv.add(R.drawable.f9);
73 // liv.add(R.drawable.f10);
74
75 final GridAdapter im=new GridAdapter();
76
77 gv_1.setAdapter(im);
78
79 gv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
80 @Override
81 public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
82
83 final View view1=View.inflate(TestActivity10.this,R.layout.gridadapter_view,null);
84
85 new AlertDialog.Builder(TestActivity10.this)
86 .setTitle("请操作...")
87 .setView(view1)
88 .setPositiveButton("修改", new DialogInterface.OnClickListener() {
89 @Override
90 public void onClick(DialogInterface dialog, int which) {
91
92 EditText et_4 = (EditText) view1.findViewById(R.id.et_4);
93
94 String string2 = et_4.getText().toString();
95
96 myClass.setName(string2);
97
98
99 EditText et_3 = (EditText) view1.findViewById(R.id.et_3);
100
101 String string1 = et_3.getText().toString();
102
103 String f9 = "f9", f10 = "f10";
104
105 if (string1.equals(f9)) {
106
107 myClass.setImg(R.drawable.f9);
108 } else if (string1.equals(f10)) {
109 myClass.setImg(R.drawable.f10);
110 }
111 else {
112
113 if (string2.length() == 0 && string1.length() == 0) {
114
115 Toast.makeText(TestActivity10.this, "请至少修改一项", Toast.LENGTH_SHORT).show();
116 }
117 }
118
119 }
120 })
121 .setNegativeButton("取消", null)
122 .show();
123
124
125 }
126 });
127
128 //长按删除的监听器
129 gv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
130 @Override
131 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
132
133 //1.删除数据
134 // 从集合里删除长按的数据
135 lm.remove(position);
136
137 //2.刷新视图,看到效果
138 // 通知适配器,数据源改变了
139 im.notifyDataSetChanged();
140
141 Toast.makeText(TestActivity10.this, "删除了记录索引="+position, Toast.LENGTH_SHORT).show();
142
143 return true;//事件已经被消费,不要在处理了
144 }
145 });
146
147
148 }
149
150 public void bt10_OnClick(View v)
151 {
152
153 final View view1=View.inflate(TestActivity10.this,R.layout.gridadapter_view,null);
154
155 new AlertDialog.Builder(TestActivity10.this)
156 .setTitle("请操作...")
157 .setView(view1)
158 .setPositiveButton("添加",null)
159 .setNegativeButton("取消", null)
160 .show();
161
162 }
163
164
165 class MyClass
166 {
167 private int img;
168
169 private String name;
170
171
172 public int getImg() {
173 return img;
174 }
175
176 public void setImg(int img) {
177 this.img = img;
178 }
179
180 public String getName() {
181 return name;
182 }
183
184 public void setName(String name) {
185 this.name = name;
186 }
187
188 public MyClass(int img,String name)
189 {
190 this.img=img;
191 this.name=name;
192 }
193 }
194
195
196 // //适配器
197 //
198 // class ImageAdapter extends BaseAdapter
199 // {
200 // @Override
201 // public int getCount() {
202 // return liv.size();
203 // }
204 //
205 // @Override
206 // public Object getItem(int position) {
207 // return liv.get(position);
208 // }
209 //
210 // @Override
211 // public long getItemId(int position) {
212 // return 0;
213 // }
214 //
215 // @Override
216 // public View getView(int position, View convertView, ViewGroup parent) {
217 //
218 // //1. 得到数据 图片的id
219 // Integer ivid = liv.get(position);
220 //
221 // //2.准备View
222 // if (convertView == null) {
223 // convertView = new ImageView(TestActivity10.this);
224 // }
225 //
226 // //3.适配
227 // // 转换
228 // ImageView imageView = (ImageView) convertView;
229 //
230 // imageView.setImageResource(ivid);
231 //
232 //// imageView.setMaxWidth(70);
233 //// imageView.setMaxHeight(70);
234 //
235 // imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
236 //
237 //
238 // return imageView;
239
240 class GridAdapter extends BaseAdapter
241 {
242 @Override
243 public int getCount() {
244 return lm.size();
245 }
246
247 @Override
248 public Object getItem(int position) {
249 return lm.get(position);
250 }
251
252 @Override
253 public long getItemId(int position) {
254 return 0;
255 }
256
257 @Override
258 public View getView(int position, View convertView, ViewGroup parent) {
259 MyClass myClass=lm.get(position);
260
261 if (convertView==null)
262 {
263 Log.e("TAG","适配器getView="+position);
264
265 convertView=View.inflate(TestActivity10.this,R.layout.listview_layout,null);
266 }
267
268 ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_3);
269 imageView.setImageResource(myClass.getImg());
270
271 TextView textView=(TextView)convertView.findViewById(R.id.tv_9);
272 textView.setText(myClass.getName());
273
274
275 return convertView;
276 }
277 }
278
279 }