Я пытаюсь создать игру с сеткой 9x9 с помощью GridView. Каждый элемент в сетке является TextView.
Я могу установить начальные значения каждого элемента в сетке в методе getView () на «0», однако я хочу изменить значение каждой сетки в отдельности после этого, но не смог этого сделать.
Я попытался добавить функцию update () в мой расширенный класс GridAdapter, который принимает позицию и число для обновления в этой позиции, но это, похоже, не работает.
public void update(int position, int number) {
TextView cell;
cell = (TextView) getItem(position);
if (cell != null)
{
cell.setText(Integer.toString(number));
}
}
Кто-нибудь знает, как этого можно достичь?
Вот весь класс GridAdapter, если требуется,
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));
}
}
}