Domanda

I am trying to implement parceable to pass data to the next screen so on my home activity I am calling a bunch of methods (oncreate, etc.) now I am trying to take from the query made in the oncreate and pass it into the next screen on a onclick, however prior to the onclick I want to add the information by telling parceable to take care of it... However I am not sure how I can pass the information from the onclick into a new class that implements Parceable...

Here is my code attempt: (not sure what I pass into the extra, documentation is lacking on android website)

@Override
    public void onClick(View v) {
        if (v == add_new_dictionary) {
            Log.e("clicked add button", "adding the dictionary... ");
            // no return true because it is void on intents
            Intent add_new_dictionary_intent = new Intent(MainActivity.this,
                    add_new_dictionary.class);
            startActivity(add_new_dictionary_intent);

        } else {
            // used to get tag for dictionary
            Object tag = v.getTag();

            //call parceable
            SendIdOfDictionary(tag);

            // start new intent based on the tag -
            Intent new_dictionary_view = new Intent(MainActivity.this,
                    dictionary_view.class);
            new_dictionary_view.putExtra("d_id", WhatAmISupposeToPassInHere);
            startActivity(new_dictionary_view);

        }

        // parceable is faster than serialization - create class to implement it

    }


    private void SendIdOfDictionary(Object tag) {
        class ParceDictionaryData implements Parcelable {
            private String tag;

            /* everything below here is for implementing Parcelable */

            // 99.9% of the time you can just ignore this
            public int describeContents() {
                return 0;
            }

            // write your object's data to the passed-in Parcel
            public void writeToParcel(Parcel out, int flags) {
                out.writeString(tag);
            }

            // this is used to regenerate your object. All Parcelables must have
            // a CREATOR that implements these two methods
            public final Parcelable.Creator<ParceDictionaryData> CREATOR = new Parcelable.Creator<ParceDictionaryData>() {
                public ParceDictionaryData createFromParcel(Parcel in) {
                    return new ParceDictionaryData(in);
                }

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

            // example constructor that takes a Parcel and gives you an object
            // populated with it's values
            private ParceDictionaryData(Parcel in) {
                tag = in.readString();
            }
        }
È stato utile?

Soluzione

Use a bundle, no need for a Parceable here.

Add it to the Intent that starts your 2nd Activity:

Intent i = new Intent();
i.putExtra("tag", yourTag);

Then get it with

getIntent().getExtras().getString("tag");

Altri suggerimenti

to pass the values

public void onClick(View view) {
 //Launching All products Activity
 Intent in = new Intent(getApplicationContext(), ViewMap.class);
// sending address to next activity
in.putExtra("address",strAdd);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}

and to receive the value

  Intent i = getIntent();
// getting product id (pid) from intent
Bundle extras = getIntent().getExtras();
if (extras != null) {
adress=extras.getString("address");    
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top