热门标签 | 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);

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



推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • IneedtofocusTextCellsonebyoneviaabuttonclick.ItriedlistView.ScrollTo.我需要通过点击按钮逐个关注Tex ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
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社区 版权所有