给GridView提供点击按钮添加新数据,单击项目修改,长按删除功能
activity_practise7的layout文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp2.PractiseActivity7"
android:orientation="vertical"> <GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gv_1"
android:numColumns="4"
android:stretchMode="columnWidth"
android:gravity="center"
android:horizontalSpacing="30dp"></GridView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击添加"
android:onClick="bt_tj_onClick"/>
</LinearLayout>
activity_practise7_1的layout文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_3"
android:gravity="center"
android:hint="从a,b,c,d里面选择图片"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_4"
android:hint="给图片命名"
android:gravity="center"/>
</LinearLayout>
gridview_layout的layout文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:layout_width="110dp"
android:layout_height="110dp"
android:id="@+id/iv_3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_3"/>
</LinearLayout>
java类:
package com.hanqi.testapp2; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class PractiseActivity7 extends AppCompatActivity { GridView gv_1;
List<MyClass> liv;
TextView tv_3;
ImageView iv_3;
final ImageAdapter imageAdapter = new ImageAdapter(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise7);
gv_1 = (GridView)findViewById(R.id.gv_1);
tv_3 = (TextView)findViewById(R.id.tv_3);
iv_3 = (ImageView)findViewById(R.id.iv_3); //1、数据 图片的id
liv = new ArrayList<>();
final MyClass myClass = new MyClass(R.drawable.f1,"美食1的介绍");
liv.add(myClass);
liv.add(new MyClass(R.drawable.f2,"美食2的介绍"));
liv.add(new MyClass(R.drawable.f3,"美食3的介绍"));
liv.add(new MyClass(R.drawable.f4,"美食4的介绍"));
liv.add(new MyClass(R.drawable.f5,"美食5的介绍"));
liv.add(new MyClass(R.drawable.f6,"美食6的介绍"));
liv.add(new MyClass(R.drawable.f7,"美食7的介绍"));
liv.add(new MyClass(R.drawable.f8,"美食8的介绍")); gv_1.setAdapter(imageAdapter); gv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final MyClass myClass = liv.get(position); //这一步非常重要,要不然不知道修改的哪部分
LayoutInflater layoutInflater = getLayoutInflater();
final View view1 = layoutInflater.inflate(R.layout.activity_practise7_1, null);
new AlertDialog.Builder(PractiseActivity7.this)
.setTitle("修改内容")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et_3=(EditText)view1.findViewById(R.id.et_3);
String string1=et_3.getText().toString();
EditText et_4 = (EditText)view1.findViewById(R.id.et_4);
String string2 = et_4.getText().toString();
String a="a",b="b",c="c",d="d";
if (string1.equals(a))
{
if (string2.length() ==0)
{
myClass.setImg(R.drawable.anniu01);
}
else
{
myClass.setImg(R.drawable.anniu01);
myClass.setContent(string2);
}
}
else if (string1.equals(b))
{
if (string2.length() ==0)
{
myClass.setImg(R.drawable.anniu02);
}
else
{
myClass.setImg(R.drawable.anniu02);
myClass.setContent(string2);
}
}
else if (string1.equals(c))
{
if (string2.length() ==0)
{
myClass.setImg(R.drawable.anniu04);
}
else
{
myClass.setImg(R.drawable.anniu04);
myClass.setContent(string2);
}
}
else if (string1.equals(d))
{
if (string2.length() ==0)
{
myClass.setImg(R.drawable.anniu05);
}
else
{
myClass.setImg(R.drawable.anniu05);
myClass.setContent(string2);
}
}
else
{
Toast.makeText(PractiseActivity7.this, "请做出至少一项修改", Toast.LENGTH_SHORT).show();
}
imageAdapter.notifyDataSetChanged();
}
})
.setView(view1)
.show();
}
}); gv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { //1.删除数据
//从集合里删除长按的数据
liv.remove(position);
//2.刷新视图,看到效果
//通知适配器,数据源改变
imageAdapter.notifyDataSetChanged();
Toast.makeText(PractiseActivity7.this, "删除了记录索引 = " + position, Toast.LENGTH_SHORT).show();
return true;//事件已经被消费,不要再处理了
}
});
}
public void bt_tj_onClick(View v)
{
final View view=View.inflate(PractiseActivity7.this, R.layout.activity_practise7_1, null);
new AlertDialog.Builder(PractiseActivity7.this)
.setTitle("添加的内容")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et_3=(EditText)view.findViewById(R.id.et_3);
String string1=et_3.getText().toString();
EditText et_4 = (EditText)view.findViewById(R.id.et_4);
String string2 = et_4.getText().toString();
String a="a",b="b",c="c",d="d";
if (string1.equals(a))
{
liv.add(new MyClass(R.drawable.anniu01,string2));
}
else if (string1.equals(b))
{
liv.add(new MyClass(R.drawable.anniu02,string2));
}
else if (string1.equals(c))
{
liv.add(new MyClass(R.drawable.anniu04,string2));
}
else if (string1.equals(d))
{
liv.add(new MyClass(R.drawable.anniu05,string2));
}
else
{
Toast.makeText(PractiseActivity7.this, "请从a,b,c,d中选择",
Toast.LENGTH_SHORT).show();
}
imageAdapter.notifyDataSetChanged();
}
})
.setView(view)
.show();
}
class MyClass
{
private int img;
private String content; public int getImg() {
return img;
} public void setImg(int img) {
this.img = img;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
public MyClass(int img,String content)
{
this.img = img;
this.content = content;
}
}
//适配器
class ImageAdapter extends BaseAdapter
{
@Override
public int getCount() {
return liv.size();
} @Override
public Object getItem(int position) {
return liv.get(position);
} @Override
public long getItemId(int position) {
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
//1.得到数据 图片的id
MyClass myClass = liv.get(position);
//2.准备View
if (convertView ==null) {
convertView = View.inflate(PractiseActivity7.this, R.layout.gridview_layout, null);
}
//3.适配
//转换
ImageView imageView = (ImageView)convertView.findViewById(R.id.iv_3);
imageView.setImageResource(myClass.getImg()); TextView textView = (TextView)convertView.findViewById(R.id.tv_3);
textView.setText(myClass.getContent());
return convertView;
}
} }