给GridView提供点击按钮添加新数据,单击项目修改,长按删除功能
activity_practise7的layout文件:
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.PractiseActivity7" 11 android:orientation="vertical"> 12 13 <GridView 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:id="@+id/gv_1" 17 android:numColumns="4" 18 android:stretchMode="columnWidth" 19 android:gravity="center" 20 android:horizontalSpacing="30dp"></GridView> 21 <Button 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="点击添加" 25 android:onClick="bt_tj_onClick"/> 26 </LinearLayout>
activity_practise7_1的layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <EditText 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:id="@+id/et_3" 11 android:gravity="center" 12 android:hint="从a,b,c,d里面选择图片"/> 13 <EditText 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:id="@+id/et_4" 17 android:hint="给图片命名" 18 android:gravity="center"/> 19 </LinearLayout>
gridview_layout的layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <ImageView 8 android:layout_width="110dp" 9 android:layout_height="110dp" 10 android:id="@+id/iv_3"/> 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:id="@+id/tv_3"/> 15 </LinearLayout>
java类:
1 package com.hanqi.testapp2; 2 3 import android.app.AlertDialog; 4 import android.content.DialogInterface; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.AdapterView; 11 import android.widget.BaseAdapter; 12 import android.widget.EditText; 13 import android.widget.GridView; 14 import android.widget.ImageView; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 21 public class PractiseActivity7 extends AppCompatActivity { 22 23 GridView gv_1; 24 List<MyClass> liv; 25 TextView tv_3; 26 ImageView iv_3; 27 final ImageAdapter imageAdapter = new ImageAdapter(); 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_practise7); 33 gv_1 = (GridView)findViewById(R.id.gv_1); 34 tv_3 = (TextView)findViewById(R.id.tv_3); 35 iv_3 = (ImageView)findViewById(R.id.iv_3); 36 37 //1、数据 图片的id 38 liv = new ArrayList<>(); 39 final MyClass myClass = new MyClass(R.drawable.f1,"美食1的介绍"); 40 liv.add(myClass); 41 liv.add(new MyClass(R.drawable.f2,"美食2的介绍")); 42 liv.add(new MyClass(R.drawable.f3,"美食3的介绍")); 43 liv.add(new MyClass(R.drawable.f4,"美食4的介绍")); 44 liv.add(new MyClass(R.drawable.f5,"美食5的介绍")); 45 liv.add(new MyClass(R.drawable.f6,"美食6的介绍")); 46 liv.add(new MyClass(R.drawable.f7,"美食7的介绍")); 47 liv.add(new MyClass(R.drawable.f8,"美食8的介绍")); 48 49 gv_1.setAdapter(imageAdapter); 50 51 gv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 52 @Override 53 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 54 final MyClass myClass = liv.get(position); //这一步非常重要,要不然不知道修改的哪部分 55 LayoutInflater layoutInflater = getLayoutInflater(); 56 final View view1 = layoutInflater.inflate(R.layout.activity_practise7_1, null); 57 new AlertDialog.Builder(PractiseActivity7.this) 58 .setTitle("修改内容") 59 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 60 @Override 61 public void onClick(DialogInterface dialog, int which) { 62 EditText et_3=(EditText)view1.findViewById(R.id.et_3); 63 String string1=et_3.getText().toString(); 64 EditText et_4 = (EditText)view1.findViewById(R.id.et_4); 65 String string2 = et_4.getText().toString(); 66 String a="a",b="b",c="c",d="d"; 67 if (string1.equals(a)) 68 { 69 if (string2.length() ==0) 70 { 71 myClass.setImg(R.drawable.anniu01); 72 } 73 else 74 { 75 myClass.setImg(R.drawable.anniu01); 76 myClass.setContent(string2); 77 } 78 } 79 else if (string1.equals(b)) 80 { 81 if (string2.length() ==0) 82 { 83 myClass.setImg(R.drawable.anniu02); 84 } 85 else 86 { 87 myClass.setImg(R.drawable.anniu02); 88 myClass.setContent(string2); 89 } 90 } 91 else if (string1.equals(c)) 92 { 93 if (string2.length() ==0) 94 { 95 myClass.setImg(R.drawable.anniu04); 96 } 97 else 98 { 99 myClass.setImg(R.drawable.anniu04); 100 myClass.setContent(string2); 101 } 102 } 103 else if (string1.equals(d)) 104 { 105 if (string2.length() ==0) 106 { 107 myClass.setImg(R.drawable.anniu05); 108 } 109 else 110 { 111 myClass.setImg(R.drawable.anniu05); 112 myClass.setContent(string2); 113 } 114 } 115 else 116 { 117 Toast.makeText(PractiseActivity7.this, "请做出至少一项修改", Toast.LENGTH_SHORT).show(); 118 } 119 imageAdapter.notifyDataSetChanged(); 120 } 121 }) 122 .setView(view1) 123 .show(); 124 } 125 }); 126 127 gv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 128 @Override 129 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 130 131 //1.删除数据 132 //从集合里删除长按的数据 133 liv.remove(position); 134 //2.刷新视图,看到效果 135 //通知适配器,数据源改变 136 imageAdapter.notifyDataSetChanged(); 137 Toast.makeText(PractiseActivity7.this, "删除了记录索引 = " + position, Toast.LENGTH_SHORT).show(); 138 return true;//事件已经被消费,不要再处理了 139 } 140 }); 141 } 142 public void bt_tj_onClick(View v) 143 { 144 final View view=View.inflate(PractiseActivity7.this, R.layout.activity_practise7_1, null); 145 new AlertDialog.Builder(PractiseActivity7.this) 146 .setTitle("添加的内容") 147 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 148 @Override 149 public void onClick(DialogInterface dialog, int which) { 150 EditText et_3=(EditText)view.findViewById(R.id.et_3); 151 String string1=et_3.getText().toString(); 152 EditText et_4 = (EditText)view.findViewById(R.id.et_4); 153 String string2 = et_4.getText().toString(); 154 String a="a",b="b",c="c",d="d"; 155 if (string1.equals(a)) 156 { 157 liv.add(new MyClass(R.drawable.anniu01,string2)); 158 } 159 else if (string1.equals(b)) 160 { 161 liv.add(new MyClass(R.drawable.anniu02,string2)); 162 } 163 else if (string1.equals(c)) 164 { 165 liv.add(new MyClass(R.drawable.anniu04,string2)); 166 } 167 else if (string1.equals(d)) 168 { 169 liv.add(new MyClass(R.drawable.anniu05,string2)); 170 } 171 else 172 { 173 Toast.makeText(PractiseActivity7.this, "请从a,b,c,d中选择", 174 Toast.LENGTH_SHORT).show(); 175 } 176 imageAdapter.notifyDataSetChanged(); 177 } 178 }) 179 .setView(view) 180 .show(); 181 } 182 class MyClass 183 { 184 private int img; 185 private String content; 186 187 public int getImg() { 188 return img; 189 } 190 191 public void setImg(int img) { 192 this.img = img; 193 } 194 195 public String getContent() { 196 return content; 197 } 198 199 public void setContent(String content) { 200 this.content = content; 201 } 202 public MyClass(int img,String content) 203 { 204 this.img = img; 205 this.content = content; 206 } 207 } 208 //适配器 209 class ImageAdapter extends BaseAdapter 210 { 211 @Override 212 public int getCount() { 213 return liv.size(); 214 } 215 216 @Override 217 public Object getItem(int position) { 218 return liv.get(position); 219 } 220 221 @Override 222 public long getItemId(int position) { 223 return 0; 224 } 225 226 @Override 227 public View getView(int position, View convertView, ViewGroup parent) { 228 //1.得到数据 图片的id 229 MyClass myClass = liv.get(position); 230 //2.准备View 231 if (convertView ==null) { 232 convertView = View.inflate(PractiseActivity7.this, R.layout.gridview_layout, null); 233 } 234 //3.适配 235 //转换 236 ImageView imageView = (ImageView)convertView.findViewById(R.id.iv_3); 237 imageView.setImageResource(myClass.getImg()); 238 239 TextView textView = (TextView)convertView.findViewById(R.id.tv_3); 240 textView.setText(myClass.getContent()); 241 return convertView; 242 } 243 } 244 245 }