Writing an Android app in Android Studio, I've got a GridView to display a set of images / text and I'm having trouble working out how to access a specific item within the list.
If I tap on an item, I can do things to it (in the code below I change the image displayed), but I can't figure out how to access a particular item (eg to change the image displayed) programmatically.
Initially I used an ImageAdapter from an example, but then I worked out if I changed it to use a TextView I could assign text to each as well as an image and it would all work well with Talkback (the Android screen reader).
I also initially added the OnTouchListener to the activity but I don't think I need that now as I just need to implement it in the adapter.
Finally I'm a bit confused on exactly what I need to be trying to access here - am I trying to get the item out of the GridView, or out of the ImageAdapter? I think the GridView sets the layout and defines where to place each item in the set and the ImageAdapter is a container for an array of items that defines the properties of each item in the set, is that right?
Kind regards
Quentin.
public class multitap extends Activity implements View.OnTouchListener {
    private GridView myGridView;
    public int numRows;
    public int numCols;
    public int currentChoice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multitap);
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                numRows = 1;
                numCols = 1;
            } else {
                numRows = extras.getInt("NUMBER_OF_GAME_ROWS");
                numCols = extras.getInt("NUMBER_OF_GAME_COLS");
            }
        } else {
            numRows = (int) savedInstanceState.getSerializable("NUMBER_OF_GAME_ROWS");
            numCols = (int) savedInstanceState.getSerializable("NUMBER_OF_GAME_COLS");
        }
        myGridView = (GridView)
                findViewById(R.id.myGridView);
        myGridView.setNumColumns(numCols);
        myGridView.setAdapter(new ImageAdapter(this));
        // now that everything is initialised, choose a random image tile.
        Random r = new Random();
        choice = r.nextInt(numCols*numRows);
    // This is the main thing I can't figure out - what do I need to replace "DoSomethingToFindItem" with?
    chosenItem = DoSomethingToFindItem(choice);
        // change the image of the chosen tile.
        chosenItem.setBackgroundResource(R.drawable.newImage);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // turns out I don't need this as onTouch in adapter collects touch event
        return false;
    }
    public class ImageAdapter extends BaseAdapter {
        private Context mContext;
    //not really sure what mConext is?
        public ImageAdapter(Context c) {
            mContext = c;
        }
        public int getCount() { return numCols*numRows; }
        public Object getItem(int position) {
        // Is this right? All of the examples on the net use "return null" claiming they it's not needed (for their example)
            return this;
        }
        public long getItemId(int position) {
            // I know I should return something other than position here, but don't know what?
            // again every example doesn't feel the need for this function.
            return position;
        }
        // create a new TextView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                textView = new TextView(mContext);
            } else {
                textView = (TextView) convertView;
            }
            textView.setBackgroundResource(R.drawable.blankcell);
            textView.setText("" + (position + 1));
            textView.setTextColor(getResources().getColor(android.R.color.transparent));
            textView.setId(position);
            textView.setMaxHeight(parent.getHeight() / numRows);
            textView.setMinimumHeight(parent.getHeight() / numRows);
            textView.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    int position;
                    position = v.getId();
                    // looking to get the first touch of any finger - this part works.
                    if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)) {
                        TextView textView;
                        if (v == null) {  // if it's not recycled, initialize some attributes
                            textView = new TextView(mContext);
                        } else {
                            textView = (TextView) v;
                        }
                        // I can set a specific image by clicking on it.
                        textView.setBackgroundResource(R.drawable.clickedImage);
                        return true;
                    } else return false;
                }
                ;});
            return textView;
        }
    }
}
 
     
    