android 添加arraylist,Java-将ArrayList对象添加到Android中的GridView

这篇博客详细介绍了如何在Android中创建一个自定义的ArrayAdapter,用于在GridView中显示ArrayList数据。适配器类`Benildus_Adapter`扩展了ArrayAdapter,并实现了getView方法来填充每个单元格。博客还展示了如何在Person类中存储和获取姓名、简历等信息。最后,演示了如何在活动中设置适配器并将其关联到GridView。
摘要由CSDN通过智能技术生成

您的适配器

public class Benildus_Adapter extends ArrayAdapter {

ArrayList list; // your person arraylist

Context context; // the activity context

int resource; // this will be your xml file

public Benildus_Adapter(Context context, int resource,ArrayList objects) {

super(context, resource, objects);

// TODO Auto-generated constructor stub

this.list = objects;

this.context = context;

this.resource = resource;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

if(list.size() == 0){

return 0;

}else{

return list.size();

}

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

View child = convertView;

RecordHolder holder;

LayoutInflater inflater = ((Activity) context).getLayoutInflater(); // inflating your xml layout

if (child == null) {

child = inflater.inflate(resource, parent, false);

holder = new RecordHolder();

holder.fname = (TextView) child.findViewById(R.id.fname); // fname is the reference to a textview

holder.lname = (TextView) child.findViewById(R.id.lname); // in your xml layout file

holder.bio =(TextView) child.findViewById(R.id.bio); // you are inflating.etc

child.setTag(holder);

}else{

holder = (RecordHolder) child.getTag();

}

final Person user = list.get(position); // you can remove the final modifieer.

holder.fname.setText(user.getFName());

holder.lname.setText(user.getLName());

holder.bio.setText(user.getBiography());

holder.image.setImageBitmap(user.getImage()); // if you use string then you download the image using

// the string as url and set it to your imageview..

return child;

}

static class RecordHolder {

TextView fname,lname,bio;

ImageView image;

}

@Override

public void notifyDataSetChanged() { // you can remove this..

// TODO Auto-generated method stub

if(getCount() == 0){

//show layout or something that notifies that no list is in..

}else{

// this is to make sure that you can call notifyDataSetChanged in any place and any thread

new Handler(getContext().getMainLooper()).post(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

Benildus_Adapter.super.notifyDataSetChanged();

}

});

}

}

}

你的人班

public class Person {

private String FName;

private String LName;

private String Biography;

private Bitmap image; // add the image too to your class, you can store the url of the image

// or save it using bitmap.. if you store the url then = String image; the load the image

// in the getview method.. any way you choose..

public String getFName() {

return FName;

}

public void setFName(String fName) {

FName = fName;

}

public String getLName() {

return LName;

}

public void setLName(String lName) {

LName = lName;

}

public String getBiography() {

return Biography;

}

public void setBiography(String biography) {

Biography = biography;

}

public Bitmap getImage() {

return image;

}

public void setImage(Bitmap image) {

this.image = image;

}

}

编辑我只是忘了设置适配器.

gridView = (GridView)findViewById(R.id.grid);

Benildus_Adapter bA = new Benildus_Adapter(this, R.layout.myxml,dbPersons);

gridView.setAdapter(bA);

希望对您有所帮助,让我知道

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值