passing a list of data from one intent to another

2019-01-29 07:38发布

问题:

I would like to parse a list of data from one intent to another intent

However, with my code, I'm only manage to pass the first item in the listview . I use a log.d to check for the items, and realised that only one items is being pass. In my listview there's 2 items,when I pass it over to my next intent, only one item was shown in the log..

I did a serializable in my class. I make a log in my summary, however, when I click on summary, the log that was shown in not all the data.

logcat:

01-28 18:20:49.218: D/Bundle(20278): bundle : Bundle[{clickedpreOdometer=, clickedID=2, clickedCost= 12.0, clickedDate=27/12/2014, pojoArrayList=[com.example.fuellogproject.fuelLogPojo@43bf3f18, com.example.fuellogproject.fuelLogPojo@43bf5b68], clickedPump=3, clickedPrice=4, clickedFCon= 0.0, clickedOdometer=3}]

listview

    public void summaryClick (View v)
{
    Intent sum = new Intent(this, summary.class);
    fuelLogPojo clickedObject = pojoArrayList.get(0);
    Bundle dataBundle = new Bundle();
    dataBundle.putString("clickedID", clickedObject.getid());
    dataBundle.putString("clickedDate", clickedObject.getdate());
    dataBundle.putString("clickedPrice", clickedObject.getprice());
    dataBundle.putString("clickedPump", clickedObject.getpump());
    dataBundle.putString("clickedCost", clickedObject.getcost());
    dataBundle.putString("clickedOdometer", clickedObject.getodometer());
    dataBundle.putString("clickedpreOdometer",
            clickedObject.getpreodometer());
    dataBundle.putString("clickedFCon", clickedObject.getfcon());
    dataBundle.putSerializable("pojoArrayList", pojoArrayList);

    Log.i("FuelLog", "dataBundle " + dataBundle);
    // Attach the bundled data to the intent
//  sum.putExtras(dataBundle);
    sum.putExtras(dataBundle);
    Log.i("Exrrass", "dataBundle " + dataBundle);
    // Start the Activity
    startActivity(sum);


}

summary.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summary);
        //month = (TextView)findViewById(R.id.month);
        avgPrice = (TextView)findViewById(R.id.showavfPriceTV);
        exFuel = (TextView)findViewById(R.id.showexFuelTV);
        avgFC = (TextView)findViewById(R.id.showavgFCTV);
        doneButton = (Button)findViewById(R.id.doneBTN);
        exitButton = (Button)findViewById(R.id.exitBTN);

        Bundle takeBundledData = getIntent().getExtras();  
        // First we need to get the bundle data that pass from the UndergraduateListActivity

        bundleID = takeBundledData.getString("clickedID");
        /*bundleDate = takeBundledData.getString("clickedDate");
        bundlePrice = takeBundledData.getString("clickedPrice");
        bundlePump = takeBundledData.getString("clickedPump");
        bundleCost = takeBundledData.getString("clickedCost");
        bundleOdometer = takeBundledData.getString("clickedOdometer");
        bundlePreOdometer = takeBundledData.getString("clickedpreOdometer");
        bundleFcon = takeBundledData.getString("clickedFCon");*/
        Log.d("Bundle","bundle : "+ takeBundledData);


}

fuelLogpojo.java

public class fuelLogPojo implements Serializable{

回答1:

fuelLogPojo should implement either Parcelable or Serializable

Bundles can accept custom classes, if they implement either Parcelable or Serializable, Parcelable is faster but more work to implement and Serializable is easier to implement, but slower.

I'm going to imagine that fuelLogPojo extends Serializable in this example, just because its easier to setup but you should really consider Parcelable

Then you can do this:

dataBundle.putSerializable("pojoArrayList", pojoArrayList);
sum.setArguments(bundle);

Also, you should reconsider the naming convention for your classes.

EDIT: Here's how to access that pojoArrayList in summary.

List<fuelLogPojo> pojoArrayList = (List<fuelLogPojo>)extras.getSerializable("pojoArrayList");


回答2:

If you want to pass array of Custom Objects You have to implements Parcerable interface

Take a look here

And then pass array like

Intent i=...
i.putParcelableArrayListExtra("ARRAY", array);

and read it

getIntent().getParcelableArrayListExtra("ARRAY")


回答3:

get the values of textbox and store it to some variables ex, val1 and val2 then use

  Intent intent = new Intent(getBaseContext(), SecondActivity.class);
  intent.putExtra("Textbox_val1", val1);
  intent.putExtra("Textbox_val2", val2);
  startActivity(intent);

In the second Activity write

   Bundle extras = getIntent().getExtras(); 
   if(extras !=null) {
   String value1 = extras.getString(Textbox_val1);
   String value2 = extras.getString(Textbox_val2);
   }