自定义适配器

时间:2015-11-11 20:18:53

标签: java android adapter onitemclicklistener android-adapterview

这是我收到的错误:

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

为什么我不能这样做?我已粘贴自定义View课程中存在的Adapter班级:

@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;
    }

我想在适配器中的一个onItemClickListener项目上设置listView。为什么我不这样做?我已经定义了listView

这里的错误
ListView listView = (ListView)convertView.findViewById(android.R.id.list);

我该怎么办?所有常规ClickListener都可以工作,但我希望用户能够在适配器本身中选择listView项。

编辑(答案):

问题解决了!只需将onItemClickListener放在主要活动onCreate初始化自定义Adapter的方法中。

    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();
        }
    });

2 个答案:

答案 0 :(得分:2)

findViewById方法查找子项:

  

查找具有给定ID的子视图。如果此视图具有给定的id,请返回此视图。

renderer.shadowMap.enabled = true;ListView的父级,因此convertView返回null。

为什么不在{1}}或findViewById中设置要创建布局的AdapterView.OnItemClickListener

答案 1 :(得分:0)

convertView变量是listView中的一行,因为这个方法是findViewById。

convertView.findViewById()

始终返回null。

如果你想这样做,你可以这样做。

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);
            }
        });