您好,我正在一个项目中,我必须将“ Person”类型的ArrayList中的“ Person”类型的每个对象插入到Eclipse Android中的GrIDvIEw中.我的意图是显示带有该人姓名的图片(类似于Instagram).到目前为止,这是我尝试过的方法,但这似乎不起作用,但我很难理解.
你们有更好的解决方案吗?
ArrayList<Person> dbPersons = dop.getPersons(dop); //This is where I populate my Listint[] imageID = { R.drawable.image1, R.drawable.image2, R.drawable.image3 }; grIDVIEw = (GrIDVIEw)findVIEwByID(R.ID.grID); grIDVIEw.setAdapter(new function.CustomGrID(this, dbPersons, imageID)); grIDVIEw.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { // Todo auto-generated method stub } });
我的人的课程通常包含:
private String Fname;private String Lname;private String Biography;
无论如何,我不想用错误的代码轰炸这篇文章,因为我正在寻找一种更清洁,更好的选择.我只想将名称显示为grIDvIEw项的标题及其下的图片,并对arrayList中的其余对象执行相同的 *** 作.你能帮我个忙吗:)
解决方法:
您的适配器
public class Benildus_Adapter extends ArrayAdapter<Person> {ArrayList<Person> List; // your person arrayListContext context; // the activity contextint resource; // this will be your xml filepublic Benildus_Adapter(Context context, int resource,ArrayList<Person> objects) { super(context, resource, objects); // Todo auto-generated constructor stub this.List = objects; this.context = context; this.resource = resource;}@OverrIDepublic int getCount() { // Todo auto-generated method stub if(List.size() == 0){ return 0; }else{ return List.size(); }}@OverrIDepublic 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; }@OverrIDepublic 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);
希望对您有所帮助,让我知道
总结以上是内存溢出为你收集整理的Java-将ArrayList对象添加到Android中的GridView全部内容,希望文章能够帮你解决Java-将ArrayList对象添加到Android中的GridView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)