如何绑定ArrayList的对象到Android GridView的? [英] How to bind ArrayList Objects to android GridView?
本文介绍了如何绑定ArrayList的对象到Android GridView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数组列表对象的集合,如:
的ArrayList<&人GT;人=新的ArrayList<&人GT;();
所以,我怎么可以绑定对象名单人
到Android GridView控件
解决方案
GridView控件settingGrid;
ArrayList的<&人GT;者;
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.setting); 人=新的ArrayList<&人GT;(); settingGrid =(GridView控件)findViewById(R.id.settinggridview);
settingGrid.setAdapter(新SettingImageAdapter(本));
settingGrid.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图父母,视图V,INT位置,长的id){
}); 公共类SettingImageAdapter延伸BaseAdapter {
上下文MyContext; 公共SettingImageAdapter(上下文_MyContext){
MyContext = _MyContext;
} 公众诠释的getCount(){
返回persons.size();
} 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
视图V;
如果(convertView == NULL){
LayoutInflater李= getLayoutInflater();
V = li.inflate(R.layout.setting_gried_item,NULL);
TextView的电视=(TextView中)v.findViewById(R.id.grid_item_text);
tv.setText(档案+位置); tv.setText(persons.get(位置)); }
其他
{
V = convertView;
}
返回伏;
} 公共对象的getItem(INT为arg0){
返回null;
}
众长getItemId(INT为arg0){
返回0;
} //我们的图像的引用
私人整数[] = mThumbIds {
R.drawable.one,R.drawable.two,
R.drawable.five,R.drawable.four,
R.drawable.eight,R.drawable.seven,
R.drawable.seven
};
私有String [] =名称{
墙纸设置,字体设置,
Sysnchronization下载,上传Sysnchronization
更改密码,照相机,
画廊
};
}
和您的XML就像是:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /的GridItem
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:重力=CENTER_HORIZONTAL
机器人:layout_x =201px
机器人:layout_y =165像素>
< TextView的机器人:ID =@ + ID / grid_item_text
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=TextView的
机器人:重力=CENTER_HORIZONTAL
机器人:文字颜色=#000000>
< / TextView的>< / LinearLayout中>
这是Setting.xml的
<?XML版本=1.0编码=UTF-8&GT?; < GridView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:背景=@绘制/壁纸
机器人:ID =@ + ID / settinggridview
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:为numColumns =auto_fit
机器人:textFilterEnabled =真
机器人:stretchMode =columnWidth时
机器人:比重=中心
机器人:horizontalSpacing =15像素
机器人:verticalSpacing =25像素
>
试试这样
I've a collection of array list objects like:
ArrayList<Person> persons=new ArrayList<Person>();
so how can i bind this list of objects persons
to android gridview
?
解决方案
GridView settingGrid;
ArrayList<Person> persons;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
persons =new ArrayList<Person>();
settingGrid = (GridView)findViewById(R.id.settinggridview);
settingGrid.setAdapter(new SettingImageAdapter(this));
settingGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
});
public class SettingImageAdapter extends BaseAdapter{
Context MyContext;
public SettingImageAdapter(Context _MyContext){
MyContext = _MyContext;
}
public int getCount() {
return persons.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.setting_gried_item, null);
TextView tv = (TextView)v.findViewById(R.id.grid_item_text);
tv.setText("Profile "+position);
tv.setText(persons.get(position));
}
else
{
v = convertView;
}
return v;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.one, R.drawable.two,
R.drawable.five, R.drawable.four,
R.drawable.eight,R.drawable.seven,
R.drawable.seven
};
private String[] names = {
"Wallpaper Setting","Font Setting",
"Sysnchronization Download","Sysnchronization Upload",
"Change Password","Camera",
"Gallery"
};
}
And your XML like be :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_x="201px"
android:layout_y="165px" >
<TextView android:id="@+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#000000">
</TextView>
</LinearLayout>
This is setting.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/wallpaper"
android:id="@+id/settinggridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:textFilterEnabled="true"
android:stretchMode="columnWidth"
android:gravity="center"
android:horizontalSpacing="15px"
android:verticalSpacing="25px"
>
try like this
这篇关于如何绑定ArrayList的对象到Android GridView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文