如何将 ArrayList 对象绑定到 android GridView? [英] How to bind ArrayList Objects to android GridView?

查看:29
本文介绍了如何将 ArrayList 对象绑定到 android GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数组列表对象,例如:

I've a collection of array list objects like:

ArrayList<Person> persons=new ArrayList<Person>();

那么我如何将这个对象列表 persons 绑定到 android gridview?

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"
    };
}

你的 XML 就像是:

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>

这是setting.xml

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"
>

试试这个

这篇关于如何将 ArrayList 对象绑定到 android GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆