每60秒刷新Fragment1中的Json并更新Fragment2中的数据

时间:2015-01-12 12:13:59

标签: json fragment refresh fragmenttransaction

大家好!

我有两个片段:Fragment1和Fragment2; Fragment1包含Json,在获取Json之后,我通过.replace(..)将Fragment1替换为Fragment2并通过Bundle传输Json。

我的目标是:例如每60秒在Fragment1中刷新Json并自动更新Fragment2,但我不知道该怎么做!我需要你的帮助!!

这是我的代码:    Class Fragment1

public class Fragment1 extends Fragment implements OnClickListener{
  public static final String IMAGE_RESOURCE_ID="iconResourceID";
  public static final String ITEM_NAME="itemName";
  Button btnvalider;
  //test transfer variable entre fragment
  public Communicator com;
  public void setCom(Communicator com) {
    this.com = com;
  }
   /*
    * Test Jsonparser
    * 
   */
  Context c;
  private ProgressDialog pDialog;
  public String testfinalewa="";
  JSonParser jsonParser = new JSonParser();
  // url to create new product
  private static String url_create_product = "http://10.0.2.2/webservice/create_personne.php";

  public String getTestfinalewa() {
    return testfinalewa;
  }
  public void setTestfinalewa(String testfinalewa) {
     this.testfinalewa = testfinalewa;
  }
  // JSON Node names
  private static final String TAG_SUCCESS = "success";
 //fin test json


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    c=getActivity();

    return inflater.inflate(R.layout.fragment_1, container, false);
 }
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onActivityCreated(savedInstanceState);
   btnvalider=(Button)getActivity().findViewById(R.id.button1);
   btnvalider.setOnClickListener(this);
   this.com=(Communicator) getActivity();
 }
 @Override
 public void onClick(View v) {
   // TODO Auto-generated method stub
   new Handler().postDelayed(new Runnable() {
   public void run() {
    // call JSON methods here
         new AttemptLogin().execute();

    }
 }, 1  );



 }
    //interface pour transferer variable entre fragment
 public interface Communicator{
      public void respond(String data);
 }
 class AttemptLogin extends AsyncTask<String, String, String>{//<params,progress,result>
 boolean failure = false;

 @Override
 protected void onPreExecute() {
      super.onPreExecute();

      pDialog = new ProgressDialog(c);
      pDialog.setMessage("Chargement...");
      pDialog.setIndeterminate(false);
      pDialog.setCancelable(true);
      pDialog.show();

 }

 @Override
 protected String doInBackground(String... args) {
        String name ="test";// pseudo.getText().toString();
        String moddepasse = "test";//mdp.getText().toString();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        Log.i("misy ve", "ok="+moddepasse);
        Log.i("misy ve", "ok="+name);
        params.add(new BasicNameValuePair("moddepasse", moddepasse));
        Log.i("test", "mbola tena mety eto 1");
        // getting JSON Object

        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);


        Log.d("Create Response", json.toString());
       Log.i("test", "mbola tena mety eto");

        try {
            int success = json.getInt(TAG_SUCCESS);
            String succ=json.getString("ok");

            Log.i("milay", succ);

            if ((success == 1)) {

                Log.i("accepter", "mdp correct");

            } else {
                Log.i("pas accepter", "non correct");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
       return json.toString();
    }

 protected void onPostExecute(String result) {
     pDialog.dismiss();
     com.respond(result);
     Fragment2 fb=new Fragment2();
     FragmentTransaction t=getFragmentManager().beginTransaction();
     Bundle args=new Bundle();
     args.putString("mondata", result);
     fb.setArguments(args);
     t.replace(R.id.myFramePrincipal, fb).commit();


 }


 }
}

class Fragment2

 public class Fragment2 extends Fragment{
     TextView text; 
     String aa;
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        text=(TextView) getActivity().findViewById(R.id.textView1);
        text.setText(getArguments().getString("mondata"));

     }
     public void refreshData(String data) {
       aa= new String(data);
      //aa.notifyDataSetChanged();
       text.setText(aa);
     }
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       return inflater.inflate(R.layout.fragment_3, container, false);
     }
     public void ChangerText(String data) {
       text.setText(data);
     }
  }

感谢您的帮助!!

0 个答案:

没有答案