【问题标题】:Adding ArrayList Objects to GridView in Android在 Android 中将 ArrayList 对象添加到 GridView
【发布时间】:2014-12-14 17:10:46
【问题描述】:

您好,我正在开发一个项目,我必须将“Person”类型的 ArrayList 中的“Person”类型的每个对象插入到 Eclipse android 中的 Gridview 中。我的意图是显示一张图片以及人名(类似于 Instagram)。到目前为止,这是我尝试过的,但这似乎并没有像我想要的那样起作用,而且很难理解。

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

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 的类一般包含:

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

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

【问题讨论】:

  • @Jedil 我认为您的链接已失效.. 但是 Benildus,您想要根据您的情况为您编写代码>?
  • @Elltz 不,伙计,只需一个与我的情况相似的样本就足够了。那时我可以通过它。我只想要一个示例,其中包含对象的 arrayList 填充在 gridview 中:)
  • 是的,对不起,我实际上是在等待那封电子邮件的邮件,但它从来没有收到,所以 idk,你希望我宁愿邮寄吗?所以给我发邮件,我希望 pjayness 作为用户名,k,没有难过的感觉。

标签: java android eclipse gridview arraylist


【解决方案1】:

您的适配器

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

} 

你的person类

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

希望对你有帮助,告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2013-10-29
    • 2011-12-24
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多