GridView OnItemClickListener does not call onItemClick

I am trying to configure OnItemClickListener for elements in my gridview. For some reason, the onItemCLick method in the listener is never called.

Listener and adapter settings:

UsersAdapter usersAdapter = new UsersAdapter(venueUsers); gridView.setAdapter(usersAdapter); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Intent intent = new Intent(Users.this, com.roqbot.client.login.Profile.class); intent.putExtra("idUser", id); startActivity(intent); } }); 

My adapter:

 private class UsersAdapter extends BaseAdapter implements ListAdapter { private JSONArray users; private UsersAdapter(JSONArray users) { this.users = users; } public int getCount() { return users.length(); } public JSONObject getItem(int position) { return users.optJSONObject(position); } public long getItemId(int position) { return users.optJSONObject(position).optInt("idUser"); } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = Users.this.getLayoutInflater().inflate(R.layout.user_icon, null); JSONObject user = getItem(position); TextView username = (TextView) convertView.findViewById(R.id.username); username.setText(user.optString("sName")); TextView userScore = (TextView) convertView.findViewById(R.id.userScore); int iDJScore = user.optInt("iDJScore"); if (iDJScore > 0) { userScore.setText(Integer.toString(iDJScore)); } else { userScore.setVisibility(TextView.INVISIBLE); ((ImageView) convertView.findViewById(R.id.userScoreBg)).setVisibility(View.INVISIBLE); } TextView userLevel = (TextView) convertView.findViewById(R.id.userLevel); userLevel.setText(user.optString("iDJLevel")); TextView userMatch = (TextView) convertView.findViewById(R.id.userMatch); ImageView matchIcon = (ImageView) convertView.findViewById(R.id.matchIcon); int iCompatibility = user.optInt("iCompatibility"); if (iCompatibility != 0) { userMatch.setText( iCompatibility + "%"); } else { userMatch.setVisibility(TextView.INVISIBLE); matchIcon.setVisibility(ImageView.INVISIBLE); } ImageView userIcon = (ImageView) convertView.findViewById(R.id.userIcon); String sUserIcon = user.optString("sImageUrl-thumb"); imageLoader.DisplayImage(sUserIcon, Users.this, userIcon); return convertView; } } 

I am rather puzzled by why the click listener is not working. This code works in many other places for me.

+4
source share
2 answers

I had the same problem.

In my situation, the click and click event of the android event: focusable = true

In My GridList Layout

 <TextView android:id="@+id/auction_item_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" ***android:focusable="true"*** android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" /> 

I delete [android: focusable = "true"], the problem is solved

+6
source

In fact, you just need to make sure that the elements in the grid are not clickable. GridViews cannot handle interactive elements. Buttons, for example, will not work. And also, if you created an element from LinearLayout or any other element without a click, you must make sure that you have not set the property: clickable = "true".

+3
source

Source: https://habr.com/ru/post/1385050/


All Articles