我有一组数组列表对象,如:
ArrayList<Person> persons=new ArrayList<Person>();
那么如何将这个对象列表persons
绑定到android gridview
?
答案 0 :(得分:2)
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"
};
}
你的XML就像:
<?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>
这是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"
>
试试这个