Get element View from a Gridview


Last Updated:

  1. johnnybeem

    johnnybeem New Member This Topic's Starter

    Joined:
    Aug 9, 2010
    Messages:
    4
    Likes Received:
    0
    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 (Text):
    1. /** Called when the activity is first created. */
    2.     @Override
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         setContentView(R.layout.main);
    6.        
    7.         // setup the adapter for showing letters
    8.         GridView gridview = (GridView) findViewById(R.id.gridview);
    9.        
    10.         // create the array of letters
    11.         ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String,String>> ();
    12.  
    13.         for (int i=0; i<16; i++) {
    14.             HashMap<String,String> map = new HashMap<String,String>();
    15.             map.put("lettertext", new Character((char)(65+i)).toString());
    16.             data.add(map);
    17.         }
    18.        
    19.         SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.letter,
    20.                 new String[]{"lettertext"}, new int[]{R.id.lettertext});
    21.  
    22.         gridview.setAdapter(adapter);
    23.  
    24.         //Object v = adapter.getView(0, lettertext, gridview);
    25.         //Object v = gridview.getChildAt(0);
    26.         //Object v = adapter.getItem(0);
    27.     }
     

    Advertisement
  2. johnnybeem

    johnnybeem New Member This Topic's Starter

    Joined:
    Aug 9, 2010
    Messages:
    4
    Likes Received:
    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 :/
     
  3. johnnybeem

    johnnybeem New Member This Topic's Starter

    Joined:
    Aug 9, 2010
    Messages:
    4
    Likes Received:
    0
    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
     

Share This Page

Loading...