问题
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
?
回答1:
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
来源:https://stackoverflow.com/questions/6921870/how-to-bind-arraylist-objects-to-android-gridview