如何将模型类parcelable对象从片段传递给activity?

时间:2016-03-24 05:05:07

标签: java android android-fragments

我想将Parcelable模型类对象从片段传递给activity。在片段中,我传递了AsyncTask postExecute()方法。在活动中,我需要在listview中追加模型类对象dat。我尝试使用arraylist和Serializable但是它不起作用。请提出如何制作的建议。

My Fragment课程:

public class PlantPOSearch extends Fragment {

Button go;
PlantPOModel plantPOModel = new PlantPOModel();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.plantpo_search, container, false);
    go = (Button) rootView.findViewById(R.id.ordergo);


    go.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MyAsyncTask myAsyncTask = new MyAsyncTask();
            myAsyncTask.execute();
        }
    });
    return rootView;
}



class MyAsyncTask extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
    }

    protected String doInBackground(String... params)  {

        InputStream inputStream = null;
        HttpURLConnection urlConnection = null;
        String line = "", response = "";

     try
      {

          //int plantnum = Integer.parseInt(params[0]);

          String url1="http://192.168.1.5:8002/sap/opu/odata/sap/ZMM_PO_APP_SRV/POHeaderSet?$filter=Plant%20eq%20%271000%27";
          URL url = new URL(url1);
          String authorizationString = "Basic " + Base64.encodeToString(("abaper" + ":" + "erp@786").getBytes(), Base64.NO_WRAP);
          URL urlToken = new URL("http://192.168.1.5:8002/sap/opu/odata/sap/ZSALES_APP_SRV/");
          String token = new TokenGeneration().getToken(authorizationString, urlToken);

        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Authorization", authorizationString);
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Accept", "application/json");
        urlConnection.setRequestProperty("X-CSRF-Token", token);
        urlConnection.setRequestMethod("GET");

        Log.d("Token", token);
        int statusCode = urlConnection.getResponseCode();
        Log.d("Response code", "" + statusCode);

        if (statusCode == HttpsURLConnection.HTTP_OK) {
            inputStream = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = bufferedReader.readLine()) != null) {
                response += line;
            }

            if (null != inputStream) {
                inputStream.close();
            }
            Log.d("Plant Response", response);

        }

        Gson gson = new Gson();
          plantPOModel = gson.fromJson(response,PlantPOModel.class);
          Log.d("company code",plantPOModel.getD().getResults()[0].getCompanyCode());
           Log.d("Doc Number", plantPOModel.getD().getResults()[0].getPurchaseDocNo());


    } catch (Exception e) {
        e.printStackTrace();
    }

       return null;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
        try {

            Intent intent = new Intent(getActivity().getApplicationContext(), POList.class);


            ArrayList<PlantPOModel> arrayList = new ArrayList<>();
            arrayList.add(plantPOModel);
            intent.putParcelableArrayListExtra("Plant",arrayList);
            startActivity(intent);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

} 

我的模特课:

public class PlantPOModel implements Parcelable{

public DClass getD() {
    return d;
}

public void setD(DClass d) {
    this.d = d;
}

private DClass d;

public class DClass {


    public Results[] getResults() {
        return results;
    }

    public void setResults(Results[] results) {
        this.results = results;
    }

    private Results[] results;

    public class Results{

        public Metadata get_metadata() {
            return _metadata;
        }

        public void set_metadata(Metadata _metadata) {
            this._metadata = _metadata;
        }

        private Metadata _metadata;

        public class Metadata{


            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getUri() {
                return uri;
            }

            public void setUri(String uri) {
                this.uri = uri;
            }

            public String getType() {
                return type;
            }

            public void setType(String type) {
                this.type = type;
            }

            private String id;
            private String uri;
            private String type;


        }

        public String getPurchaseDocNo() {
            return PurchaseDocNo;
        }

        public void setPurchaseDocNo(String purchaseDocNo) {
            PurchaseDocNo = purchaseDocNo;
        }

        public String getCompanyCode() {
            return CompanyCode;
        }

        public void setCompanyCode(String companyCode) {
            CompanyCode = companyCode;
        }

        public String getDocType() {
            return DocType;
        }

        public void setDocType(String docType) {
            DocType = docType;
        }

        public String getCreatedBy() {
            return CreatedBy;
        }

        public void setCreatedBy(String createdBy) {
            CreatedBy = createdBy;
        }

        public String getVendor() {
            return Vendor;
        }

        public void setVendor(String vendor) {
            Vendor = vendor;
        }

        public String getPurchaseOrg() {
            return PurchaseOrg;
        }

        public void setPurchaseOrg(String purchaseOrg) {
            PurchaseOrg = purchaseOrg;
        }

        public String getPurchaseGrp() {
            return PurchaseGrp;
        }

        public void setPurchaseGrp(String purchaseGrp) {
            PurchaseGrp = purchaseGrp;
        }

        public String getDocDate() {
            return DocDate;
        }

        public void setDocDate(String docDate) {
            DocDate = docDate;
        }

        public String getInco1() {
            return Inco1;
        }

        public void setInco1(String inco1) {
            Inco1 = inco1;
        }

        public String getInco2() {
            return Inco2;
        }

        public void setInco2(String inco2) {
            Inco2 = inco2;
        }

        public String getRelGrp() {
            return RelGrp;
        }

        public void setRelGrp(String relGrp) {
            RelGrp = relGrp;
        }

        public String getRelStr() {
            return RelStr;
        }

        public void setRelStr(String relStr) {
            RelStr = relStr;
        }

        public String getRelInd() {
            return RelInd;
        }

        public void setRelInd(String relInd) {
            RelInd = relInd;
        }

        public String getRelStatus() {
            return RelStatus;
        }

        public void setRelStatus(String relStatus) {
            RelStatus = relStatus;
        }

        public String getMaterial() {
            return Material;
        }

        public void setMaterial(String material) {
            Material = material;
        }

        public String getPlant() {
            return Plant;
        }

        public void setPlant(String plant) {
            Plant = plant;
        }

        private String PurchaseDocNo;
        private String CompanyCode;
        private String DocType;
        private String CreatedBy;
        private String Vendor;
        private String PurchaseOrg;
        private String PurchaseGrp;
        private String DocDate;
        private String Inco1;
        private String Inco2;
        private String RelGrp;
        private String RelStr;
        private String RelInd;
        private String RelStatus;
        private String Material;
        private String Plant;
        private POItemSet POItemSet;

        public POItemSet getPOItemSet() {
            return POItemSet;
        }

        public void setPOItemSet(POItemSet POItemSet) {
            this.POItemSet = POItemSet;
        }

        public class POItemSet{

            public Deffered get_deferred() {
                return _deferred;
            }

            public void set_deferred(Deffered _deferred) {
                this._deferred = _deferred;
            }

            private Deffered _deferred;



            public class Deffered {

                private String uri;

                public String getUri() {
                    return uri;
                }

                public void setUri(String uri) {
                    this.uri = uri;
                }
            }

        }

    }

}
@Override
public void writeToParcel(Parcel dest, int flags) {

}

public int describeContents(){
    return 0;
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public PlantPOModel createFromParcel(Parcel in) {
        return new PlantPOModel();
    }

    public PlantPOModel[] newArray(int size) {
        return new PlantPOModel[size];
    }

};

}

我的活动课程:

public class POList extends AppCompatActivity {

ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
private ListView listView;
POListAdapter poListAdapter;

POMaterialModel poMaterialModel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.polist);




    ArrayList<PlantPOModel> arrayList = getIntent().getParcelableArrayListExtra("Plant");

    PlantPOModel plantPOModel = arrayList.get(0);

    try{
         Log.d("company code",plantPOModel.getD().getResults()[0].getCompanyCode());
    } catch (Exception e){
        e.printStackTrace();
    }

    listView = (ListView) findViewById(R.id.listView);

    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            //startActivity(new Intent(POList.this,POListView.class));
        }
    });



}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.logout) {
        finish();
        startActivity(new Intent(this, Login.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

0 个答案:

没有答案