package com.example.wang.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
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 Main2Activity extends AppCompatActivity {
GridView gv_1;
List<MyClass> lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
gv_1=(GridView)findViewById(R.id.gv_1);
lm=new ArrayList<MyClass>();
MyClass myClass=new MyClass(R.drawable.f1,"名称1");
lm.add(myClass);
lm.add(new MyClass(R.drawable.f2,"名称2"));
lm.add(new MyClass(R.drawable.f3,"名称3"));
lm.add(new MyClass(R.drawable.f4,"名称4"));
lm.add(new MyClass(R.drawable.f5,"名称5"));
lm.add(new MyClass(R.drawable.f6,"名称6"));
lm.add(new MyClass(R.drawable.f7,"名称7"));
lm.add(new MyClass(R.drawable.f8,"名称8"));
lm.add(new MyClass(R.drawable.f9,"名称9"));
lm.add(new MyClass(R.drawable.f10,"名称10"));
lm.add(new MyClass(R.drawable.f10,"名称1111"));
lm.add(new MyClass(R.drawable.f10,"名称11"));
GridAdapter gridAdapter=new GridAdapter();
gv_1.setAdapter(gridAdapter);
}
class GridAdapter extends BaseAdapter
{
@Override
public int getCount() {
return lm.size();
}
@Override
public Object getItem(int position) {
return lm.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyClass myClass=lm.get(position);
if (convertView==null)
{
Log.e("TAG", "适配器 getView=" + position);
convertView=View.inflate(Main2Activity.this,R.layout.activity_main21,null);
}
ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_1);
imageView.setImageResource(myClass.getImg());
TextView textView=(TextView)convertView.findViewById(R.id.tv_1);
if (myClass.getName().length()>=5||myClass.getName().trim().length()==0)
{
Toast.makeText(Main2Activity.this, myClass.getName()+"不符合命名规则,请重新命名", Toast.LENGTH_LONG).show();
textView.setText("");
}
else
{
textView.setText(myClass.getName());
}
return convertView;
}
}
class MyClass
{
private int img;
private String name;
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyClass(int img,String name)
{
this.img=img;
this.name=name;
}
}
}