Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView OnItemClickListener is not calling onItemClick

I am trying to set up an OnItemClickListener for the items in my Gridview. For some reason the onItemCLick method within the listener is never called.

Setting the listener and the adapter:

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 pretty baffled as to why the click listener isn't working. This code works in a lot of other places for me.

like image 829
dbaugh Avatar asked Jan 19 '23 03:01

dbaugh


2 Answers

I had same problem.

In my situation, "android:focusable = true" block item click event

In My GridList Item 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 remove [ android:focusable="true" ], problem is solved

like image 166
ChangUZ Avatar answered Jan 29 '23 02:01

ChangUZ


Actually, you just need to make sure that the items in the grid aren't clickable. GridViews can't handle clickable items. Buttons for example won't work. And also if you made an item from a LinearLayout, or any other non-clickable item, you have to make sure that you didn't set the property: clickable="true".

like image 20
Daan van Hulst Avatar answered Jan 29 '23 02:01

Daan van Hulst