J
J
Julia Vorobieva2017-07-05 20:24:29
Java
Julia Vorobieva, 2017-07-05 20:24:29

How to access nth item of GridView from adapter?

Good day to all :)
I created a GridView with the number of (large) buttons I needed. I need that by clicking on one of the buttons, I could change the parameters of the adjacent or any other button found by position - for example, make it unpressable (it sounds kind of crooked in Russian, but oh well). I process pressing directly inside the adapter (I don’t know how true this is, since I’m just starting to write for android). The adapter is custom, inherited from BaseAdapter. The getView method in my implementation now looks like this:

@Override
        public View getView(final int position, final View convertView, final ViewGroup parent) {
            View grid = convertView;
            if(convertView == null ) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                grid = inflater.inflate(R.layout.item_battle_field, parent, false);
            } else {
                grid = convertView;
            }
            final ImageButton  imageButton = grid.findViewById(R.id.item_battle_field);
            imageButton.setImageResource(R.drawable.battle_field_color);
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(mContext, String.valueOf(position),    Toast.LENGTH_SHORT).show();
                }
            });
            return grid;
        }

That is, by clicking, I simply display the position number now. How to reach other buttons from OnClick?
Thank you in advance:)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2017-07-05
@aol-nnov

I would have the properties needed in the model and change them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question