关于listview视图的 作业
代码运行目录
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.testapp"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity" /> 12 <activity android:name=".test_activity6" /> 13 <activity android:name=".TestActivity7" /> 14 <activity android:name=".TestActivity8" /> 15 <activity android:name=".Main2Activity" /> 16 <activity android:name=".TestActivity9" /> 17 <activity android:name=".TestActivity10" /> 18 <activity android:name=".TestActivity11"> 19 </activity> 20 <activity android:name=".TsetActivity12"> 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN" /> 23 <category android:name="android.intent.category.LAUNCHER" /> 24 </intent-filter> 25 </activity> 26 </application> 27 28 </manifest>
布局文件
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 <ImageView 7 android:layout_width="70dp" 8 android:layout_height="70dp" 9 android:id="@+id/iv_1"/> 10 <TextView 11 android:layout_width="70dp" 12 android:layout_height="wrap_content" 13 android:gravity="center" 14 android:id="@+id/tv_1"/> 15 </LinearLayout>
表格布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 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.example.wang.myapplication.Main2Activity"> 11 <GridView 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:id="@+id/gv_1" 15 android:numColumns="4" 16 android:gravity="center" 17 android:horizontalSpacing="20dp"> 18 </GridView> 19 </RelativeLayout>
自定义对话框布局文件
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="50dp" 10 android:layout_marginTop="5dp" 11 android:gravity="center" 12 android:hint="请从f1~f10中选择一张图" 13 android:id="@+id/et_2" /> 14 <EditText 15 android:layout_width="match_parent" 16 android:layout_height="50dp" 17 android:layout_marginTop="5dp" 18 android:gravity="center" 19 android:hint="请给图片命名" 20 android:id="@+id/et_1" /> 21 </LinearLayout>
JAVA运行程序
1 package com.example.administrator.testapp; 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.util.Log; 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 TsetActivity12 extends AppCompatActivity { 22 23 GridView gv_1; 24 List<MyClass> lm; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main2); 30 31 gv_1=(GridView)findViewById(R.id.gv_1); 32 33 lm=new ArrayList<MyClass>(); 34 35 MyClass myClass=new MyClass(R.drawable.f1,"名称1"); 36 37 lm.add(myClass); 38 lm.add(new MyClass(R.drawable.f2,"名称2")); 39 40 lm.add(new MyClass(R.drawable.f3,"名称3")); 41 42 lm.add(new MyClass(R.drawable.f4,"名称4")); 43 44 lm.add(new MyClass(R.drawable.f5,"名称5")); 45 46 lm.add(new MyClass(R.drawable.f6,"名称6")); 47 48 lm.add(new MyClass(R.drawable.f7,"名称7")); 49 50 lm.add(new MyClass(R.drawable.f8,"名称8")); 51 52 lm.add(new MyClass(R.drawable.f9,"名称9")); 53 54 final GridAdapter gridAdapter=new GridAdapter(); 55 56 gv_1.setAdapter(gridAdapter); 57 58 gv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 59 60 @Override 61 public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 62 63 final View view1=View.inflate(TsetActivity12.this, R.layout.activity_main4, null); 64 65 new AlertDialog.Builder(TsetActivity12.this) 66 .setView(view1) 67 .setNegativeButton("添加", new DialogInterface.OnClickListener() { 68 @Override 69 public void onClick(DialogInterface dialog, int which) { 70 71 EditText et_1=(EditText)view1.findViewById(R.id.et_1); 72 73 String str12=et_1.getText().toString(); 74 75 if (str12.length()>=5||str12.trim().length()==0) 76 { 77 Toast.makeText(TsetActivity12.this, "不符合命名规则,请重新命名", 78 Toast.LENGTH_SHORT).show(); 79 80 return; 81 } 82 83 EditText et_2=(EditText)view1.findViewById(R.id.et_2); 84 85 String str22=et_2.getText().toString(); 86 87 String a="a",aa="aa",aaa="aaa",aaaa="aaaa"; 88 89 if (str22.equals(a)) 90 { 91 lm.add(new MyClass(R.drawable.f1,str12)); 92 } 93 else if (str22.equals(aa)) 94 { 95 lm.add(new MyClass(R.drawable.f2,str12)); 96 } 97 else if (str22.equals(aaa)) 98 { 99 lm.add(new MyClass(R.drawable.f3,str12)); 100 } 101 else if (str22.equals(aaaa)) 102 { 103 lm.add(new MyClass(R.drawable.f4,str12)); 104 } 105 else 106 { 107 Toast.makeText(TsetActivity12.this, "请从a,aa,aaa,aaaa中选择", 108 Toast.LENGTH_SHORT).show(); 109 } 110 111 gridAdapter.notifyDataSetChanged(); 112 } 113 }) 114 .setNeutralButton("取消",null) 115 .setPositiveButton("修改", new DialogInterface.OnClickListener() { 116 @Override 117 public void onClick(DialogInterface dialog, int which) { 118 MyClass myClass = lm.get(position); 119 120 EditText et_1 = (EditText) view1.findViewById(R.id.et_1); 121 122 String str12 = et_1.getText().toString(); 123 124 if (str12.length() >= 5) { 125 Toast.makeText(TsetActivity12.this, "不符合命名规则,请重新命名", 126 Toast.LENGTH_SHORT).show(); 127 return; 128 } 129 130 EditText et_2 = (EditText) view1.findViewById(R.id.et_2); 131 132 String str22 = et_2.getText().toString(); 133 134 String a = "a", aa = "aa", aaa = "aaa", aaaa = "aaaa"; 135 136 if (str22.equals(a)) { 137 if (str12.length() == 0) { 138 myClass.setImg(R.drawable.f1);//只修改图片 139 } else { 140 myClass.setImg(R.drawable.f1); 141 myClass.setName(str12); 142 } 143 } else if (str22.equals(aa)) { 144 if (str12.length() == 0) { 145 myClass.setImg(R.drawable.f2); 146 } else { 147 myClass.setImg(R.drawable.f2); 148 myClass.setName(str12); 149 } 150 } else if (str22.equals(aaa)) { 151 if (str12.length() == 0) { 152 myClass.setImg(R.drawable.f3); 153 } else { 154 myClass.setImg(R.drawable.f3); 155 myClass.setName(str12); 156 } 157 } else if (str22.equals(aaaa)) { 158 if (str12.length() == 0) { 159 myClass.setImg(R.drawable.f4); 160 } else { 161 myClass.setImg(R.drawable.f4); 162 myClass.setName(str12); 163 } 164 } else if (str22.length() == 0) { 165 if (str12.length() == 0) { 166 Toast.makeText(TsetActivity12.this, "请至少修改一项内容", Toast.LENGTH_SHORT).show(); 167 } else { 168 myClass.setName(str12); 169 } 170 } else { 171 Toast.makeText(TsetActivity12.this, "请从a,aa,aaa,aaaa中选择", Toast.LENGTH_SHORT).show(); 172 } 173 174 gridAdapter.notifyDataSetChanged(); 175 } 176 }) 177 .show(); 178 } 179 }); 180 181 gv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 182 @Override 183 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 184 lm.remove(position); 185 gridAdapter.notifyDataSetChanged(); 186 return true; 187 } 188 }); 189 190 } 191 192 class GridAdapter extends BaseAdapter 193 { 194 @Override 195 public int getCount() { 196 return lm.size(); 197 } 198 199 @Override 200 public Object getItem(int position) { 201 return lm.get(position); 202 } 203 204 @Override 205 public long getItemId(int position) { 206 return 0; 207 } 208 209 @Override 210 public View getView(int position, View convertView, ViewGroup parent) { 211 212 MyClass myClass=lm.get(position); 213 214 if (convertView==null) 215 { 216 Log.e("TAG", "适配器 getView=" + position); 217 convertView=View.inflate(TsetActivity12.this,R.layout.activity_main4,null); 218 } 219 ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_1); 220 221 imageView.setImageResource(myClass.getImg()); 222 223 TextView textView=(TextView)convertView.findViewById(R.id.tv_1); 224 225 if (myClass.getName().length()>=5||myClass.getName().trim().length()==0) 226 { 227 Toast.makeText(TsetActivity12.this, myClass.getName()+"不符合命名规则,请重新命名", 228 Toast.LENGTH_LONG).show(); 229 230 textView.setText(""); 231 } 232 else 233 { 234 textView.setText(myClass.getName()); 235 } 236 237 return convertView; 238 } 239 } 240 241 class MyClass 242 { 243 private int img; 244 245 private String name; 246 247 248 public int getImg() { 249 return img; 250 } 251 252 public void setImg(int img) { 253 this.img = img; 254 } 255 256 public String getName() { 257 return name; 258 } 259 260 public void setName(String name) { 261 this.name = name; 262 } 263 264 public MyClass(int img,String name) 265 { 266 this.img=img; 267 this.name=name; 268 } 269 } 270 }