I have a GridView with a custom adapter that is not updating.
In the debugger, I see that getView not called after notifyDataSetChanged . I have no idea why ... I can see that the underlying data is changing, but nothing is happening with the GridView.
I tried several solutions that did not work, so I am posting what in my opinion should be correct (although this is clearly not ...)
This is in my main activity
private GridView grid; private TileGridAdapter gridAdapter; private ArrayList<Tile> list; private GameManager gameManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gameManager = new GameManager(4); list = (ArrayList<Tile>) gameManager.getTiles(); grid = (GridView) findViewById(R.id.grid); gridAdapter = new TileGridAdapter(this, list); grid.setAdapter(gridAdapter); grid.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) { public void onSwipeTop() { gameManager.move(Direction.Up); list.clear(); list.addAll(gameManager.getTiles()); gridAdapter.notifyDataSetChanged(); } public void onSwipeRight() { Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show(); } public void onSwipeLeft() { Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show(); } public void onSwipeBottom() { Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show(); } }); }
And custom adapter
public class TileGridAdapter extends ArrayAdapter<Tile> { Context context; ArrayList<Tile> tiles; public TileGridAdapter(Context context, ArrayList<Tile> tiles) { super(context, R.layout.cell_layout, tiles); this.context = context; this.tiles = tiles; } @Override public int getCount() { return tiles.size(); } @Override public Tile getItem(int position) { return tiles.get(position); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); view = inflater.inflate(R.layout.cell_layout, parent, false); Tile tile = tiles.get(position); TextView tv = (TextView) view.findViewById(R.id.cell_view); if (tile != null) { tv.setText(String.valueOf(tile.getValue())); } } return view; } }
source share