How to access individual items in Android GridView?
Posted
by source.rar
on Stack Overflow
See other posts from Stack Overflow
or by source.rar
Published on 2010-06-13T12:57:37Z
Indexed on
2010/06/13
13:02 UTC
Read the original article
Hit count: 163
Hi, I'm trying to create a game with a 9x9 grid with GridView. Each item in the grid is a TextView. I am able to set the initial values of each item in the grid within the getView() method to "0", however I want to change the value of each grid individually after this but have been unable to do so.
I tried adding an update() function in my extended GridAdapter class that takes a position and a number to update at that position but this doesnt seem to be working.
public void update(int position, int number) { TextView cell; cell = (TextView) getItem(position); if (cell != null) { cell.setText(Integer.toString(number)); } }
Doe anyone know how this can be achieved?
Here's the whole GridAdapter class in case require, public class SudokuGridAdapter extends BaseAdapter { private Context myContext; private TextView[] myCells;
public SudokuGridAdapter(Context c) {
myContext = c;
myCells = new TextView[9*9];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 9*9;
}
@Override
public Object getItem(int position) {
return myCells[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView cell;
if (myCells[position] == null)
{
cell = myCells[position] = new TextView(myContext);
cell.setText("0");
}
else
{
cell = myCells[position];
}
return cell;
}
public void update(int position, int number) {
TextView cell;
cell = (TextView) getItem(position);
if (cell != null)
{
cell.setText(Integer.toString(number));
}
}
}
© Stack Overflow or respective owner