gpt4 book ai didi

java - 自定义适配器中的 setOnItemClickListener

转载 作者:行者123 更新时间:2023-11-29 20:17:01 26 4
gpt4 key购买 nike

这是我收到的错误:

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

为什么我不能做这样的事情?我粘贴了存在于我的自定义 Adapter 类中的 View 类:

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

我想在适配器中的一个 listView 项目上设置一个 onItemClickListener。为什么我不能这样做?在我定义 listView 的地方抛出错误:

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

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

编辑(答案):

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

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

最佳答案

findViewById方法寻找 child :

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

ListViewconvertView 的父级,因此 findViewById 返回 null。

为什么不在创建布局的 ActivityFragment 中设置 AdapterView.OnItemClickListener

关于java - 自定义适配器中的 setOnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33659086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com