热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

您的适配器publicclassBenildus_AdapterextendsArrayAdapter{ArrayListlist;yourpersonarraylistConte

您的适配器

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);

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



推荐阅读
author-avatar
二比大哥大
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有