我想解析从一个意图到另一个意图的数据列表
但是,用我的代码, 我只设法传递listview中的第一项。 我使用log.d来检查项目,并意识到只有一个项目正在通过。 在我的listview中有2个项目,当我将它传递给我的下一个意图时,日志中只显示了一个项目..
我在课堂上做了一个序列化。 我记录了我的总结, 然而, 当我点击摘要时,显示的日志并非所有数据。
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}]
列表视图
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{
答案 0 :(得分:2)
fuelLogPojo
应实施Parcelable
或Serializable
Bundles可以接受自定义类,如果它们实现Parcelable
或Serializable
,Parcelable
更快但实施起来更多,Serializable
更容易实现,但速度更慢
我想在这个例子中fuelLogPojo extends Serializable
,只是因为它更容易设置,但你应该考虑Parcelable
然后你可以这样做:
dataBundle.putSerializable("pojoArrayList", pojoArrayList);
sum.setArguments(bundle);
Also, you should reconsider the naming convention for your classes.
编辑:
以下是如何在摘要中访问pojoArrayList
。
List<fuelLogPojo> pojoArrayList = (List<fuelLogPojo>)extras.getSerializable("pojoArrayList");
答案 1 :(得分:0)
如果要传递自定义对象数组,则必须实现Parcerable
接口
看看here
然后传递数组
Intent i=...
i.putParcelableArrayListExtra("ARRAY", array);
并阅读
getIntent().getParcelableArrayListExtra("ARRAY")
答案 2 :(得分:0)
获取文本框的值并将其存储到某些变量ex,val1和val2,然后使用
Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("Textbox_val1", val1);
intent.putExtra("Textbox_val2", val2);
startActivity(intent);
在第二个活动中写
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value1 = extras.getString(Textbox_val1);
String value2 = extras.getString(Textbox_val2);
}