How to fill cells of a GridView with lists of names?

戏子无情 提交于 2019-12-13 02:53:59

问题


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

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.

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?

Thanks very much!


回答1:


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.

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);

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

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Parameters

context The current context.

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

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

objects The objects to represent in the 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.

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.

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.



来源:https://stackoverflow.com/questions/23903214/how-to-fill-cells-of-a-gridview-with-lists-of-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!