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

时间:2014-09-25 10:45:19

标签: android

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的数据库中获取值我想将此值传递给另一个actvity,以便我可以比较它但是当我传递给另一个时我得到空值:

我这样使用:

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请帮助建议我在哪里做错了请告诉我如何将值从一个传递给另一个

2 个答案:

答案 0 :(得分:0)

我认为你的receiverId没有被初始化,因为当你在另一个Activity中使用它时,如果没有调用getView()方法。您可以使用Broadcast传递您的receiverId。

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

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

然后,你可以通过注册一个BroadcastReceiver来接收另一个Activity中的receiverId并用它做你想做的事情:

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

答案 1 :(得分:0)

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