Java-将ArrayList对象添加到Android中的GridView
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java-将ArrayList对象添加到Android中的GridView,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4089字,纯文字阅读大概需要6分钟。
内容图文
![Java-将ArrayList对象添加到Android中的GridView](/upload/InfoBanner/zyjiaocheng/691/0504bcdde2c24f98adda02ad624190a8.jpg)
您好,我正在一个项目中,我必须将“ 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
}
});
我的人的课程通常包含:
private String FName;
private String LName;
private String Biography;
无论如何,我不想用错误的代码轰炸这篇文章,因为我正在寻找一种更清洁,更好的选择.我只想将名称显示为gridview项的标题及其下的图片,并对arraylist中的其余对象执行相同的操作.你能帮我个忙吗:)
解决方法:
您的适配器
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;
}
}
编辑我只是忘了设置适配器.
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所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。