1

I have three activities in my application. A -> B -> C

In Activity A: I am getting some data from user and add those data NameValuePair. I do some other operations in Activity B. After that in Activity C, I want to http post NameValuePair. However, I cannot pass NameValuePair one class to another.

I tried this solution: solution

In Activity A:

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
 nameValuePairs.add(new BasicNameValuePair("username", username));
 nameValuePairs.add(new BasicNameValuePair("name", name));

 Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                intent.putParcelableArrayListExtra ("nvp", (ArrayList<? extends Parcelable>) nameValuePairs);
                startActivity(intent);
            }
    });

BasicNameValuePair.java :

public class BasicNameValuePair  implements Parcelable {
    private int mData;

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    public static final Parcelable.Creator<BasicNameValuePair> CREATOR
            = new Parcelable.Creator<BasicNameValuePair>() {
        public BasicNameValuePair createFromParcel(Parcel in) {
            return new BasicNameValuePair(in);
        }

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

    private BasicNameValuePair(Parcel in) {
        mData = in.readInt();
    }
}

I get an error in my Activity A:

Error:(57, 94) error: incompatible types: ArrayList<NameValuePair> cannot be converted to ArrayList<? extends Parcelable>

How can I pass NameValuePair another activity?

user8654574
  • 47
  • 1
  • 6
  • Change `ArrayList` to `ArrayList` – Enzokie Nov 24 '17 at 02:02
  • I get an error: `Wrong 2nd argument type. Found: 'java.util.ArrayList', required: 'java.util.ArrayList extends android.os.Parcelable> ` @Enzokie – user8654574 Nov 24 '17 at 02:14
  • @user8654574 use fully qualified name of your `BasicNameValueP‌​air` class that is class name along with it's package name. – Waqas Nov 24 '17 at 03:27
  • I am sorry I can't understand you. @Waqas – user8654574 Nov 24 '17 at 03:32
  • change `ArrayList` to `ArrayList` – Waqas Nov 24 '17 at 03:33
  • When I do what you say I get an this error: `add (com.example.ss.myapplication.BasicNameValuePair) in ArrayList cannot be applied to (org.apache.http.message.BasicNameValuePair)` @Waqas – user8654574 Nov 24 '17 at 03:40
  • You need to check why it is using `org.apache.http.message.BasicNameValuePair`. Find the reference in imports, remove it and import `com.example.ss.myapplication.BasicNameValuePair` – Waqas Nov 24 '17 at 03:42
  • Thanks! Now I have to change my `BasicNameValuePair.java` according to my data. @Waqas – user8654574 Nov 24 '17 at 09:36
  • @user8654574 try using TinyDB library for this purpose by github https://github.com/msiemens/tinydb – Tara Nov 24 '17 at 12:32

1 Answers1

0

For this problem, you have to make singleton class

that is DataHolder class

    public class DataHolder {

    public static final String TAG = "DataHolder";

    private static DataHolder mDataHolder;

    private ArrayList<NameValuePair> nameValuePairs;


    private DataHolder() {

    }

    /**
     * @return the DataHolder object. (This method returns always same object because of DataHolder is a Singleton Class.)
     */
    public static synchronized DataHolder getInstance() {
        if (null == mDataHolder) {
            mDataHolder = new DataHolder();
        }
        return mDataHolder;
    }


    public ArrayList<NameValuePair> getNameValuePairs() {
        return nameValuePairs;
    }

    public void setNameValuePairs(ArrayList<NameValuePair> nameValuePairs) {
        this.nameValuePairs = nameValuePairs;
    }
}

And then call below code for set the data

ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("name", name));
DataHolder dataHolder = DataHolder.getInstance();
dataHolder.setNameValuePairs(nameValuePairs);

& get data where you want

ArrayList<NameValuePair> nameValuePairs;
DataHolder dataHolder = DataHolder.getInstance();
nameValuePairs = dataHolder.getNameValuePairs();

I hope this will work