-1

This is one of the class where i assign data to the item and it return one parameter back to the other class. How should i write it to return two parameter instead? For this case it return final price, how should i write it to return final price and subtotal as well?

public static String Final_Price = " ";
public static String subtotal = " ";
protected String doInBackground(String... arg0) {
Final_Price = co.price;
subtotal = co.subtotal;
return Final_Price;
}
@Override
protected void onPostExecute(String result){

   String search = Final_Price;
    ((ReceiptActivity)activity).get_data(search);
}

This is the receipt activity where there is a function to get the data i pass.

public void get_data (String c)
{
shippingfeeTextView.setText("Shipping fee: " + c);
}
Cheong Charlene
  • 275
  • 1
  • 2
  • 12

4 Answers4

1

If you want only two parameters, you can use Pair. Source: http://developer.android.com/reference/android/util/Pair.html

Jonathan Darryl
  • 946
  • 11
  • 16
0

You can create your own custom object like this

public class Result{
        public String finalPrice, subTotoal;

    public Result(String st, String fp) {
        this.subTotal= st;
        this.finalPrice= fp;
    }

}

Then you can return Result object instead

Result res = new Result (x, y);
return res
varunkr
  • 5,364
  • 11
  • 50
  • 99
0

You can return it as array, object or you can create a model and pass the model as parameter.

Rashid
  • 1,700
  • 1
  • 23
  • 56
0

Assuming the two return values you want are the same data type, it's best not to over think things. Just return a simple array:

double[] finalPrice = new double[2];
    finalPrice[0] = co.price;
    finalPrice[1] = co.subtotal;
    return finalPrice;

or if you need to keep it a string:

String[] finalPrice = new String[2];
    finalPrice[0] = ""+co.price;
    finalPrice[1] = ""+co.subtotal;
    return finalPrice;

This would be the simplest and most effective way to handle returning multiple values. To use your array is simple as well:

    public void get_data (String[] c)
{
shippingfeeTextView.setText("Shipping fee: " + c[0]+"subtotal: "+c[1]);
}

If the values are different data types, just create an encapsulated data class with the appropriate fields, getters / setters, and return it's constructor:

return new CustomContainer(myDouble, anInteger);

Your get method would then look something like this:

public void get_data (CustomContainer c)
{
shippingfeeTextView.setText("Shipping fee: " + c.getSalePrice()+"subtotal:   "+c.getSubTotal());
}