在 Android 中将 ArrayList 对象添加到 GridView [英] Adding ArrayList Objects to GridView in Android

查看:25
本文介绍了在 Android 中将 ArrayList 对象添加到 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一个项目,我必须将Person"类型的 ArrayList 中Person"类型的每个对象插入到 eclipse android 中的 Gridview.我的目的是显示一张图片以及人名(类似于 Instagram).这是我到目前为止所尝试的,但这似乎并不像我想要的那样工作并且难以理解.

Hello I'm working on a project where I have to insert each object of type "Person" in an ArrayList of type "Person" to a Gridview in eclipse android. My intentions are to display a picture along with the Person's name (similar to Instagram). Here's what I have tried so far but this doesn't seem to work as I want it to and hard to understand.

你们有更好的解决方案吗?

Do yall have any better solutions?

ArrayList<Person> dbPersons = dop.getPersons(dop); //This is where I populate my list
int[] 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

                }

            });

My Person 的类一般包含:

My Person's class generally contains:

private String FName;
private String LName;
private String Biography;

无论如何,我不想用我的错误代码轰炸这篇文章,因为我正在寻找一个更干净、更好的替代方案.我只想将名称显示为 gridview 项目的标题及其下方的图片,并对数组列表中的其余对象执行相同操作.你能帮帮我吗:)

Anyway I didn't want to bombard this post with my faulty code since I'm looking for a cleaner and better alternative. I just want to display the name as the gridview item's title and the picture below it and do the same for the rest of the objects in the arraylist. Could you please help me out guys :)

推荐答案

你的适配器

 public class Benildus_Adapter extends ArrayAdapter<Person> {

ArrayList<Person> 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<Person> 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;
}

}

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

Edit i just forget the set adapter..

 gridView = (GridView)findViewById(R.id.grid);
 Benildus_Adapter bA = new Benildus_Adapter(this, R.layout.myxml,dbPersons);
 gridView.setAdapter(bA);

希望有帮助,请告诉我

这篇关于在 Android 中将 ArrayList 对象添加到 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆