Martin Erlic
Martin Erlic

Reputation: 5667

setOnItemClickListener within Custom Adapter

This is the error I receive:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference

Why can't I do something like this? I've pasted the View class that exists within my custom Adapter class:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.notifications_listview_item, null);
            holder = new ViewHolder();
            holder.username = (TextView)convertView.findViewById(R.id.username);
            holder.notificationText = (TextView)convertView.findViewById(R.id.notificationText);
            holder.time = (TextView)convertView.findViewById(R.id.time);
            holder.profilePicture = (ImageView) (convertView.findViewById(R.id.profilePicture));
            holder.notificationsIcon = (ImageView) (convertView.findViewById(R.id.notificationsIcon));

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        final ParseObject notifications = mNotifications.get(position);

        Date createdAt = notifications.getCreatedAt();
        long now = new Date().getTime();
        String convertedDate = DateUtils.getRelativeTimeSpanString(createdAt.getTime(), now, DateUtils.SECOND_IN_MILLIS).toString();

        holder.username.setText(notifications.getString(ParseConstants.KEY_SENDER_NAME));
        holder.notificationText.setText(notifications.getString(ParseConstants.KEY_NOTIFICATION_TEXT));
        holder.time.setText(convertedDate);
        holder.profilePicture = (ImageView)convertView.findViewById(R.id.profilePicture);

        if (notifications.getString(ParseConstants.KEY_NOTIFICATION_TYPE).equals(ParseConstants.TYPE_LIKE)) {
            holder.notificationsIcon.setImageResource(R.drawable.ic_action_like_feed_full);
        }

        if (notifications.getString(ParseConstants.KEY_NOTIFICATION_TYPE).equals(ParseConstants.TYPE_CYCLE)) {
            holder.notificationsIcon.setImageResource(R.drawable.ic_not_saved);
        }

        if (notifications.getString(ParseConstants.KEY_NOTIFICATION_TYPE).equals(ParseConstants.TYPE_EDIT)) {
            holder.notificationsIcon.setImageResource(R.drawable.ic_action_edit_feed);
        }

        holder.username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startGalleryActivity(notifications.getString(ParseConstants.KEY_SENDER_ID));
            }
        });

        holder.profilePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startGalleryActivity(notifications.getString(ParseConstants.KEY_SENDER_ID));
            }
        });

        holder.notificationsIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, FeedActivitySingle.class);
                intent.putExtra(FeedActivitySingle.EXTRA_DESIGN, notifications.getString(ParseConstants.KEY_SENDER_FEED_OBJECT_ID));
                mContext.startActivity(intent);
            }
        });

        ListView listView = (ListView)convertView.findViewById(android.R.id.list);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(mContext, FeedActivitySingle.class);
                    intent.putExtra(FeedActivitySingle.EXTRA_DESIGN, notifications.getString(ParseConstants.KEY_SENDER_FEED_OBJECT_ID));
                    mContext.startActivity(intent);
                }
            });

        return convertView;
    }

I want to set an onItemClickListener on one of the listView items in the Adapter. Why can't I do this? The error is thrown here where I've defined the listView:

ListView listView = (ListView)convertView.findViewById(android.R.id.list);

What should I do? All the regular ClickListeners work but I want the user to be able to select the listView item in the Adapter itself.

Edit (Answer):

Problem resolved! Just had to put my onItemClickListener in the onCreate method of the main activity where the custom Adapter was initialized.

    ListView listView = (ListView)findViewById(android.R.id.list);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "Voila! It works.", Toast.LENGTH_SHORT).show();
            List<ParseObject> notifications = adapter.getNotifications();
        }
    });

Upvotes: 1

Views: 2796

Answers (2)

The convertView variable is a row in the listView, because this the method findViewById.

convertView.findViewById()

Always return null.

if you want to do that u can do this.

convertView.setOnClicListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent=newIntent(mContext,FeedActivitySingle.class);
                intent.putExtra(FeedActivitySingle.EXTRA_DESIGN, notifications.getString(ParseConstants.KEY_SENDER_FEED_OBJECT_ID));
                mContext.startActivity(intent);
            }
        });

Upvotes: 0

Cory Charlton
Cory Charlton

Reputation: 8938

The findViewById method looks for children:

Look for a child view with the given id. If this view has the given id, return this view.

The ListView is a parent of the convertView so the findViewById is returning null.

Why not set the AdapterView.OnItemClickListener in the Activity or Fragment that you are creating the layout in?

Upvotes: 2

Related Questions