在我的應用程序中它有三個片段。每個片段都有付款按鈕,只要我們點擊付款按鈕,一個服務就會打電話。所以對於我在非活動類中編寫服務的所有片段。該服務需要來自每個片段的兩個變量。所以我把這些變量發送到非活動類如下:如何在android中從非活動類移動到活動類?
fragment1:
paymentbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Payment payment = new Payment(getActivity(),Mobilenumber,amount);
}
fragment2:
paymentbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Payment payment = new Payment(getActivity(),Mobilenumber,amount);
}
fragment3:
paymentbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Payment payment = new Payment(getActivity(),Mobilenumber,amount);
}
payment.java:
public class Payment {
JSONArray OrderItem;
String mobNumber, amount;
Context context;
String payjson, jsontopayment;
static Payment INSTANCE;
public Payment(Context context, String mobNumber, String amount) {
db = new DatabaseHandler(context);
INSTANCE = this;
this.mobNumber = mobNumber;
this.amount = amount;
new GetPayment().execute(WebUrl.RechargeServiceURL + "AddRecharge");
}
public class GetPayment extends AsyncTask<String, Void, String> {
/*String mJourneyDate;
public GetData(String pJourneyDate) {
this.mJourneyDate = pJourneyDate;
}*/
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String response;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(7);
nameValuePair.add(new BasicNameValuePair("UserUniqueID", "823166FC-83B5-4D7A-816BF81B6B0A5177"));
nameValuePair.add(new BasicNameValuePair("AccountNumber", mobNumber));
nameValuePair.add(new BasicNameValuePair("RCTypeID", rctypeid));
nameValuePair.add(new BasicNameValuePair("OperatorID", opid));
nameValuePair.add(new BasicNameValuePair("ServiceTypeID", srid));
nameValuePair.add(new BasicNameValuePair("CircleID", crid));
nameValuePair.add(new BasicNameValuePair("Amount", amount));
nameValuePair.add(new BasicNameValuePair("DeviceID", "yeryer"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
response = EntityUtils.toString(httpEntity);
// Log.d("response is", response);
return response;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
String Status;
super.onPostExecute(result);
//Log.v("TAG_RESULT",""+result);
if (result != null) {
try {
////here i want to send the result to activty class
//result have json response`enter code here`
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, "something went wrong", Toast.LENGTH_LONG).show();
}
}
}
public String getJson() {
payjson = OrderItem.toString();
Log.v("test", "test");
return payjson;
}
}
paymentActivty.java:
public class Payment_Actiivity extends Activity {
Payment pay;
String json;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
// i need the json response in this class
}
}
i got the json response in non activty class. now i need to add this json data to another activity(payment_activity). so how i get my json response data to payment_activty. now my aim is whenever i click payment button in every fragment, it need to go payment_activity. please help me out.
[安卓自定義事件監聽器(http://stackoverflow.com/questions/22881661/android-custom-event-listener)是解決問題 – 2015-02-10 08:34:39
對不起有用。我沒有得到。我新來java和andorid。請給予一些澄清 – Kartiki 2015-02-10 08:39:45