传递数据的列表从一个意图到另一个使用序列 [英] passing a list of data from one intent to another using serializable

查看:139
本文介绍了传递数据的列表从一个意图到另一个使用序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分析的数据的列表从一个意图另一个意图

不过,我的code, 我只是设法通过在ListView中的第一项。 我用一个log.d检查的项目,并认识到只有一个项目正在传递。 在我的列表视图有2项,比当我将它传递给我的下一个目标,只有一个项目在日志中显示出..

我在班上做了一个可序列化。 我做一个登录我的总结, 然而, 当我点击总之,那是不是所有的数据显示日志。

logcat的:

  01-28 18:20:49.218:D /包(20278):包:捆绑[{点击preOdometer =,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}]
 

列表视图

 公共无效summaryClick(视图v)
{
    意向金额=新的意图(这一点,summary.class);
    fuelLogPojo clickedObject = pojoArrayList.get(0);
    捆绑dataBundle =新包();
    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(点击preOdometer
            clickedObject.get preodometer());
    dataBundle.putString(clickedFCon,clickedObject.getfcon());
    dataBundle.putSerializable(pojoArrayList,pojoArrayList);

    Log.i(FuelLog,dataBundle+ dataBundle);
    //附加捆绑数据的意图
// sum.putExtras(dataBundle);
    sum.putExtras(dataBundle);
    Log.i(Exrrass,dataBundle+ dataBundle);
    //开始的活动
    startActivity(总和);


}
 

summary.java

 保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.summary);
        //月=(TextView中)findViewById(R.id.month);
        avgPrice =(TextView中)findViewById(R.id.showavfPriceTV);
        exFuel =(TextView中)findViewById(R.id.showexFuelTV);
        avgFC =(TextView中)findViewById(R.id.showavgFCTV);
        doneButton =(按钮)findViewById(R.id.doneBTN);
        exitButton =(按钮)findViewById(R.id.exitBTN);

        捆绑takeBundledData = getIntent()getExtras()。
        //首先,我们需要获得通过从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);
        捆绑preOdometer = takeBundledData.getString(点击preOdometer);
        bundleFcon = takeBundledData.getString(clickedFCon); * /
        Log.d(捆绑,捆绑+ takeBundledData);


}
 

fuelLogpojo.java

 公共类fuelLogPojo实现Serializable {
 

解决方案

fuelLogPojo 应实施任何 Parcelable 序列化

束可以接受的自定义类,如果他们要么实现 Parcelable 序列化 Parcelable 是速度更快,但更多的工作和序列化更容易实现,但速度较慢。

我会想象, fuelLogPojo扩展在这个例子中序列化,只是因为它容易设置,但你真的应该考虑 Parcelable

然后,你可以这样做:

  dataBundle.putSerializable(pojoArrayList,pojoArrayList);
sum.setArguments(包);
 

此外,你应该重新考虑的命名规则为你的类。

编辑: 以下是如何访问 pojoArrayList 的总结。

 名单,其中,fuelLogPojo> pojoArrayList =(名单< fuelLogPojo>)extras.getSerializable(pojoArrayList);
 

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{

解决方案

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

这篇关于传递数据的列表从一个意图到另一个使用序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆