繁体   English   中英

如何将值从适配器类传递到片段活动

[英]How to pass value from adapter class to fragment activity

public static String receiverId ;
public class Notifcationadapter extends BaseAdapter {

public static 
    DataBaseManager dbManager = new DataBaseManager(context);
    ArrayList<Notify> notifies;

    public Notifcationadapter(Context context, ArrayList<Notify> notifies) {
        super();
        this.context = context;
        this.notifies = notifies;

    }

    /* private view holder class */
    private class ViewHolder {

        TextView txtTitle;
        TextView txtDesc;
        ImageView yesimage;

        ImageView noimage;
        TextView revresetime;

        TextView rejected;
        ImageView notificationuserimage;
        RelativeLayout relativeLayout;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


        final DataBaseManager dbManager = new DataBaseManager(context);
        final String isRead = notifies.get(position).getNotificationIsRead();
        final String dateTime = notifies.get(position)
                .getNotificationDatetime();
        final String desc = notifies.get(position).getNotificationDescrpiton();
        final String id = notifies.get(position).getNotificationId();
        receiverId = notifies.get(position).getNotificationRecieverID();
        ((HomeActivity) context).NotifcationRecciverid = notifies.get(position)
                .getNotificationRecieverID();
        ((HomeActivity) context).nOtifcationMessge = notifies.get(position)
                .getNotificationDescrpiton();

        final String senderId = notifies.get(position)
                .getNotificationSenderID();
        final String serverId = notifies.get(position)
                .getNotificationServerId();
        final String status = notifies.get(position).getNotificationStatus();
        final String type = notifies.get(position).getNotificationType();
}
}

这是我的Adapter类的代码能够从接收方ID的数据库中获取值,我想将此值传递给另一个活动,以便我可以比较它,但是当我传递给另一个活动时却得到null值:

正在这样使用:

String value =Notifcationadapter.receiverId;
        markerOptions.title(datamodel.contactName);
        if (datamodel.id
                .equals(Notifcationadapter.receiverId)) {
            markerOptions
                    .snippet(((HomeActivity) getActivity()).nOtifcationMessge);
            map.addMarker(markerOptions).showInfoWindow();

        } else {
            markerOptions.snippet(datamodel.phoneNumber);
            // Adding marker on the Google Map
            map.addMarker(markerOptions);
        }

    }

此代码用于比较值,但在此处获取接收者ID的值null请帮助建议我哪里做错了,请告诉我如何将值从一个传递给另一个

我认为您的receiverId尚未初始化,因为在另一个Activity中使用getView()方法(如果尚未调用)。 您可以通过使用Broadcast传递您的receiveId。

收到reciverId时,可以使用以下代码发送广播:

Intent intent = new Intent("com.youpkgname.getReciverId");
intent.putExtra(key, reciverId);
context.sendBroadcast(intent);

然后,您可以通过注册BroadcastReceiver并在其中执行您想要的操作来在另一个Activity中接收receiveId:

registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                // get the receiverId from intent here, and do what you want with it
            }
}, new IntentFilter("com.youpkgname.getReciverId"));

添加view.setTag(receiverId); getView(..); view.getTag()在您需要此Hop的类中有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM