Android 使用serializable将数据列表从一个意图传递到另一个意图

Android 使用serializable将数据列表从一个意图传递到另一个意图,android,listview,android-intent,Android,Listview,Android Intent,我想将数据列表从一个意图解析为另一个意图 但是,用我的代码, 我只能设法传递listview中的第一项。 我使用log.d检查项目,并意识到只有一个项目通过。 在我的listview中有2项,当我将其传递给下一个意图时,日志中只显示了一项 我在班上做了一个连载。 我在我的总结中做了一个日志, 然而, 当我单击summary时,显示在并非所有数据中的日志 日志: 01-28 18:20:49.218: D/Bundle(20278): bundle : Bundle[{clickedpreOdom

我想将数据列表从一个意图解析为另一个意图

但是,用我的代码, 我只能设法传递listview中的第一项。 我使用log.d检查项目,并意识到只有一个项目通过。 在我的listview中有2项,当我将其传递给下一个意图时,日志中只显示了一项

我在班上做了一个连载。 我在我的总结中做了一个日志, 然而, 当我单击summary时,显示在并非所有数据中的日志

日志:

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}]
列表视图

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


}
fuelogpojo.java

public class fuelLogPojo implements Serializable{

如果要传递自定义对象数组,必须实现
parcelable
接口

看一看

然后像数组一样传递

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

getIntent().getParcelableArrayListExtra("ARRAY")

fuelogpojo
应实现
Parcelable
Serializable

捆绑包可以接受自定义类,如果它们实现
Parcelable
Serializable
Parcelable
更快,但要实现的工作量更多,
Serializable
更容易实现,但速度较慢

< P> >我想假设代码> FuelLogPojo在这个例子中扩展了可序列化的,只是因为它更容易设置,但是您应该真正考虑<代码> PARCELSET> <代码> < /P> 然后你可以这样做:

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

编辑: 下面简要介绍如何访问该
pojoArrayList

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

您最好让您的对象实现
可包裹
界面,这样您就可以传递整个对象,而不是每次传递5或6行数据。我编辑了这篇文章,总结了如何访问该列表