如何将活动中的数据发送到片段android

时间:2016-12-17 09:06:33

标签: android android-fragments android-activity

在Activity中,我调用了Web服务并获得了json响应。现在我需要在Activity的一个片段中发送这个json响应。请任何人帮助我如何将json响应发送到没有空值的片段。 提前谢谢。

public class Gifts_Activity extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.refereal_activtiy);
onSucceeded();
}
  private void onSucceeded() {
        ServiceClient serviceClient = ServiceUtil.getServiceClient();
        JsonObject json = new JsonObject();
        json.addProperty("user_id", "353");
        json.addProperty("device_id", "433");
        serviceClient.movie_quiz(json, callback);
    }

    Callback<JsonObject> callback = new Callback<JsonObject>() {
        @Override
        public void success(final JsonObject jsonObject, Response response) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                   ///here i am getting response. i need this response in fragment .

                }
            });
        }

        @Override
        public void failure(RetrofitError error) {

        }

    };
}

fragement:

public class Electronics_fragment  extends Fragment {
    Refeeral_Fragemnt.ShareAdapter adapter;
    LinearLayout lay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (container == null) {
            return null;
        }
        lay = (LinearLayout) inflater.inflate(R.layout.referralhistory, container, false);
        return lay;
    }
}

1 个答案:

答案 0 :(得分:0)

runOnUiThread(new Runnable() {

        @Override    
        public void run() {  

            //here i am getting response. i need this response in fragment .-->

            //then here you can store(set) your json response value in SharedPreferences, please define SharedPreferences method globally

            //and  after when you call fragment retrieve(get) SharedPreferences Method in your Fragment.

        }    
    });