There is a GridView to which the adapter is connected. The data in the gridview is recorded normally. The problem is this: When I process the OnItemClickListener event in the GridView, the highlighted line is highlighted. However, when I scroll a GridView, then somehow the element indices go astray, and the element I click on is not highlighted, but the element below it is highlighted (depending on how far I scrolled the GridView). When I return the scroll to its original position, the string is allocated as needed.

Screenshots for better understanding:
enter image description here
Next, I scroll down one position and wipe out the same line as in the first screenshot, but the line below it is highlighted.
Result:
enter image description here
Adapter code, and method of clicking on the GridView:

Adapter

public View getView(int position, View convertView, ViewGroup parent) { Cat cat = getItem(position); String s = cat.Uch; if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.item,null); } ((TextView) convertView.findViewById(R.id.tt)).setText(cat.Hour); ((TextView) convertView.findViewById(R.id.tt1)).setText(cat.Uch); if(s.startsWith("Свободно")) ((LinearLayout) convertView.findViewById(lay)).setBackgroundResource(R.drawable.rect1); else ((LinearLayout) convertView.findViewById(lay)).setBackgroundResource(R.drawable.rect); return convertView; } 

Push method

 gvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { if (gvmaincl==0) { fab1.startAnimation(animationbut); fab1.setVisibility(View.VISIBLE); fab2.startAnimation(animationbut); fab2.setVisibility(View.VISIBLE); } if (v1!=null) { l = (LinearLayout) v1.findViewById(R.id.lay); if (Objects.equals(t1.getText().toString(), "Свободно")) l.setBackgroundResource(R.drawable.rect1); else l.setBackgroundResource(R.drawable.rect); } v1 = gvMain.getChildAt(position); l = (LinearLayout) v1.findViewById(R.id.lay); t = (TextView) v1.findViewById(R.id.tt); t1 = (TextView) v1.findViewById(R.id.tt1); l.setBackgroundResource(R.drawable.rect2); } }); 

Tell me what the problem is and how it can be fixed.

  • And what are the values ​​of all these mysterious letters v1, t1 before entering the listener? The findViewById() method is very resource intensive to pull them with each press. Get the links to the widgets once when creating the interface (in onCreate() ) and assign them to the class fields, then refer to these fields, and do not look for the same thing each time for a new one. - pavlofff
  • Sorry for the stupid question (I'm just getting used to programming under Android), and to which class should I assign the links created on OnCreate? - Anton
  • And how can I then find the View of the clicked item in the GridView? - Anton
  • find the strength to read a couple of books: Ekkel "The Philosophy of Java" and B. Hardy "Android. Programming for Professionals" and do not have to refer to the lack of knowledge in such basic questions, this is the base. Now you do the following: to call a friend with whom you call up several times a day, every time you call the help desk, explain there who you need, wait for a while when they find it and say the phone, then dial it and talk to a friend It looks very ridiculous, but this is how you use the findViewById () method now. - pavlofff
  • Instead, it is more logical to write down the phone once to yourself and then immediately call it when you need it - when creating the UI activation, save the links to the widgets to the fields of this activation class and continue to work with them. The adapter uses the ViewHolder pattern for the same purpose. - pavlofff

0