如何填写一个GridView的细胞名称列表? [英] How to fill cells of a GridView with lists of names?

查看:173
本文介绍了如何填写一个GridView的细胞名称列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件,每一个细胞,其中我想与名称的列表来填充,我需要这些名单是动态变化的。我试图实现该想法如下:

I have a GridView, each cell of which I would like to populate with a list of names, and I need these lists to be dynamically changeable. I tried implementing this idea as follows:

public void fillBoard(int groupSize){
    int numGroups = listPresentMembers.size()/groupSize;
    ListView [] groups = new ListView [numGroups];


    for(int i=0;i<numGroups;i++){
        String [] items = new String[groupSize];
        for(int j=0;j<groupSize;j++){

          items[j] = listPresentMembers.get(i*groupSize + j);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,android.R.id.text1,items);
        groups[i] = new ListView(this);
        groups[i].setAdapter(adapter);
    }
    ArrayAdapter<ListView> groupsAdapter = new ArrayAdapter<ListView>(this,
            android.R.layout.simple_list_item_1,android.R.id.text1,groups);

    gridViewBoard.setAdapter(groupsAdapter);
}

的基本想法是,我创建一个 ArrayAdapter 类型的ListView ,这是一个定义的每个项目名称的团体,我想填充 GridView控件的特定细胞。然后我设置 ArrayAdapter 我的 GridView控件

The essential idea is that I create an ArrayAdapter of type ListView, each item of which is defined by one of the groups of names I would like to populate a particular cell of the GridView. I then set this ArrayAdapter to my GridView.

问题是,当我运行应用程序时, GridView控件的元素都是字符串,如
android.widget.ListView@41bcfa60。我去哪儿了?

The problem is that when I run the app, the elements of the GridView are strings such as "android.widget.ListView@41bcfa60". Where did I go wrong?

非常感谢!

推荐答案

简短的回答:当你看到一些像android.widget.ListView@41bcfa60,这意味着什么在你的code期待一个String和你给它的对象(在这种情况下,一个ListView)代替

Short answer: whenever you see something like "android.widget.ListView@41bcfa60", it means that something in your code was expecting a String and you gave it an object (in this case, a ListView) instead.

到底发生了什么?看看适配器为你的GridView。

So what happened? Take a look at the adapter for your GridView.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,android.R.id.text1,items);

下面是你使用一个ArrayAdapter的构造:

Here's the constructor for the ArrayAdapter you're using:

公开ArrayAdapter(上下文的背景下,INT资源,诠释textViewResourceId,T []对象)

参数

背景当前上下文。

资源包含布局实例化视图时使用的布局文件资源ID。

resource The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId 来填充版面资源中的TextView中的ID

textViewResourceId The id of the TextView within the layout resource to be populated

对象在ListView重新present的对象。

objects The objects to represent in the ListView.

您正在传递 android.R.layout.simple_list_item_1 资源,这意味着它的布局你'再使用。此布局由单一的TextView的 - 文一行。的它的目的不是要举行一个ListView。

You're passing android.R.layout.simple_list_item_1 as the resource, which means it's the layout you're using. This layout consists of a single TextView--a single line of text. It is not designed to hold a ListView.

在这个布局要填充的TextView(即 textViewResourceId ),它看起来在数组的适当元素(项目),您传递,并设法得到文本出来的 - 在Java中,我们通过调用toString()的对象上做到这一点。如果的toString()不落实,默认情况下它会返回类似的东西,你得到了android.widget.ListView@41bcfa60胡言乱语。

When this layout wants to populate the TextView (the textViewResourceId), it looks at the appropriate element of the array (items) that you passed in and tries to get text out of it--in Java we do this by calling toString() on an object. If toString() is not implemented, by default it returns something like the "android.widget.ListView@41bcfa60" gibberish that you got.

这就是为什么,但我觉得你的code有更大的问题。如果我是你,我强烈重新考虑,涉及到一个GridView,其中每个网格项是一个ListView的设计。这不是传统可言,所以可能有一个更好的方式来完成任何你正在试图做的。考虑张贴另一个问题解释你的目标和要求code设计的帮助。

That's the why, but I think your code has bigger problems. If I were you, I'd strongly reconsider a design that involves a GridView where each grid item is a ListView. That is not conventional at all, so there's probably a better way to accomplish whatever you're trying to do. Consider posting another question explaining your goal and asking for code design help.

这篇关于如何填写一个GridView的细胞名称列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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