如何将ArrayList对象绑定到android GridView?

发布于 2024-11-28 03:25:06 字数 189 浏览 2 评论 0原文

我有一个数组列表对象的集合,例如:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2024-12-05 03:25:06
  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>

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

尝试这样

  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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文