如何将NameValuePair传递给android中的另一个活动?

时间:2017-11-24 01:47:53

标签: java android basicnamevaluepair

我的申请中有三项活动。 A -> B -> C

Activity A中:我从用户那里获取一些数据并添加这些数据NameValuePair。 我在Activity B中做了一些其他的操作。在Activity C之后,我想要http发布NameValuePair。但是,我无法将NameValuePair传递给另一个类。

我尝试了这个解决方案:solution

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();
    }
}

我在Activity A:

中收到错误消息
Error:(57, 94) error: incompatible types: ArrayList<NameValuePair> cannot be converted to ArrayList<? extends Parcelable>

如何将NameValuePair传递给另一个活动?

1 个答案:

答案 0 :(得分:0)

对于这个问题, 你必须制作单身类

是的 DataHolder类

    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;
    }
}

然后调用下面的代码来设置数据

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

&安培;获取您想要的数据

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

我希望这会起作用