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

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



推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
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社区 版权所有