• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Get element View from a Gridview

Hi all,
I am new to Android development and am trying to set a simple app with a 4x4 grid of TextViews. I've been trying for a few hours now, and can't find the method that returns a "child" View from the GridView. This seems like it should be a pretty simple thing.

Basically, I set up a 4x4 GridView and all of its elements are TextViews. Right now the text in each box of the grid is a single character 'A', 'B', 'C'... up to the first 16 letters. Later in the code, I will need to access the individual views to set properties on them (i.e. background color, text, etc). I have tried all of the following, but none are working properly - the first throws an exception, the second returns null, and the third returns the HashMap for the SimpleAdapter :(

Object v = adapter.getView(0, lettertext, gridview);
Object v = gridview.getChildAt(0);
Object v = adapter.getItem(0);

Here is some slimmed down code from my onCreate function. Thanks in advance!


Code:
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // setup the adapter for showing letters
        GridView gridview = (GridView) findViewById(R.id.gridview);
        
        // create the array of letters
        ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String,String>> ();

        for (int i=0; i<16; i++) {
        	HashMap<String,String> map = new HashMap<String,String>();
        	map.put("lettertext", new Character((char)(65+i)).toString());
        	data.add(map);
        }
        
        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.letter,
        		new String[]{"lettertext"}, new int[]{R.id.lettertext});

        gridview.setAdapter(adapter);

        //Object v = adapter.getView(0, lettertext, gridview);
        //Object v = gridview.getChildAt(0);
        //Object v = adapter.getItem(0);
    }
 
Okay, made a little bit of progress...

If I do this (note the "null" argument now)...
Object v = adapter.getView(0, null, gridview);

...it no longer throws an exception and actually gives the TextView element that I ask for (in this case, the element at the zero position). But I think each time it is returning a new reference, not the reference to the actual View displaying in the UI. So when I change it's properties they do not appear on screen...

Ugh, this is a lot harder than it should be :/
 
Alright, just made my own Adapter extending BaseAdapter and then I can set whatever display preferences I want from that adapter.getView function. Guess I answered my own question but hopefully this helps somebody else out at least.

John
 
Back
Top Bottom