本文是小编为大家收集整理的关于在标签之间传递ArrayList<String>。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我不是很清楚 Intent 对象以及如何使用它在活动之间传递数据.在我的应用程序中,我有几个选项卡,我想在这些选项卡之间传递 ArrayList.这是我计划使用的示例代码,但我错过了主要活动捕获 Intent 并将其在开始时传递给新活动的部分:
1. myTabs.java ==> 这是我认为我需要添加一些代码以在 TabOne 和 TabTwo 之间传递数据的地方.目前只是使用TabActivity示例的示例代码.
public class myTabs extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Reusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, TabPeopleActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("TabOne").setIndicator("TabOne", res.getDrawable(R.drawable.ic_tab_one)) .setContent(intent); tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this, TabTransactionActivity.class); spec = tabHost.newTabSpec("TabTwo").setIndicator("TabTwo", res.getDrawable(R.drawable.ic_tab_two)) .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(0); } }
2. TabOne.java ==> 我在 onStop 过程中添加了一段代码,用我想传递给 TabTwo 的数组填充 Intent 数据.不确定这是不是正确的方法.
public class TabOne extends Activity { [...] private ArrayList<String> arrayPeople; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabone); arrayPeople = new ArrayList<String>(); [... here we modify arrayPeople ...] } /** Called when the activity looses focus **/ @Override public void onStop(){ Intent myIntent = new Intent(); myIntent.putStringArrayListExtra("arrayPeople", arrayPeople); this.setIntent(myIntent); } }
3. TabTwo.java ==> 在这里,我试图从 Activity 启动时应该传递的 Intent 中获取 ArrayList.但如何做到这一点?
public class TabTwo extends Activity { private ArrayList<String> arrayPeople; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.transaction); Intent myIntent = new Intent(); myIntent = this.getIntent(); arrayPeople = myIntent.getStringArrayListExtra("arrayPeople"); } }
感谢您的想法!
编辑:
好吧,我会说得简单点,这是项目的完整工作区:
http://www.lecompteestbon.com/Android.LCEB.zip
我想做的是网站lecompteestbon的离线版本,它允许人们在一个周末后在朋友之间做会计.
TabPeople = Add the list of friends TabTransactions = List of expenses TabTransaction = Add an expense TabResult = Calculate the list of payments
让我知道如何完成这项工作:)
推荐答案
在 myTabs 对象中设置静态字段有什么用?
public class myTabs extends TabActivity { .... public static arrayPeople = new ArrayList<String>(); ....
在 TabOne.java 上:
@Override public void onStop(){ myTabs.arrayPeople = arrayPeople; }
在 TabTwo.java 上:
arrayPeople = myTabs.arrayPeople
有意义吗?
其他推荐答案
通过上面的例子,当我替换"onStop" = "onPause"时真的很有效
/** Called when the activity looses focus **/ @Override public void onStop() { Intent myIntent = new Intent(); myIntent.putStringArrayListExtra("arrayPeople", arrayPeople); this.setIntent(myIntent); } /** Called when the activity looses focus **/ @Override public void onPause() { Intent myIntent = new Intent(); myIntent.putStringArrayListExtra("arrayPeople", arrayPeople); this.setIntent(myIntent); }
其他推荐答案
我不认为在 onStop() 中改变意图会起作用,因为 TabHost 处理意图.使用 this.setIntent() 设置一个类的意图不会让另一个类使用 getIntent() 访问它,因为它们建立在不同的意图上.
我要么将我的 arrayPeople 存储在数据库中,要么使用 MyTabs.setArrayPeople 或类似的东西将 arrayPeople 传递回 TabHost.然后您可以查询下一个选项卡的 db onCreate 或从您的 MyTabs 类中提取它.
问题描述
I'm not very clear about the Intent object and how to use it to pass data between Activities. In my application I have several tabs between which I want to pass ArrayList. Here is a sample code I plan to use, but I'm missing the part where the main activity catches the Intent and passes it to the new activity on start :
1. myTabs.java ==> This is where I think I need to add some code to pass the data between TabOne and TabTwo. For now it is just using the sample code of the TabActivity sample.
public class myTabs extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Reusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, TabPeopleActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("TabOne").setIndicator("TabOne", res.getDrawable(R.drawable.ic_tab_one)) .setContent(intent); tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this, TabTransactionActivity.class); spec = tabHost.newTabSpec("TabTwo").setIndicator("TabTwo", res.getDrawable(R.drawable.ic_tab_two)) .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(0); } }
2. TabOne.java ==> I added a piece of code in the onStop procedure to fill in the Intent data with the array I want to pass to TabTwo. Not sure it is the right way to do though.
public class TabOne extends Activity { [...] private ArrayList<String> arrayPeople; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabone); arrayPeople = new ArrayList<String>(); [... here we modify arrayPeople ...] } /** Called when the activity looses focus **/ @Override public void onStop(){ Intent myIntent = new Intent(); myIntent.putStringArrayListExtra("arrayPeople", arrayPeople); this.setIntent(myIntent); } }
3. TabTwo.java ==> Here I am trying to fetch the ArrayList from the Intent that is supposed to be passed when the Activity starts. But how to do this?
public class TabTwo extends Activity { private ArrayList<String> arrayPeople; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.transaction); Intent myIntent = new Intent(); myIntent = this.getIntent(); arrayPeople = myIntent.getStringArrayListExtra("arrayPeople"); } }
Thanks for your ideas !
EDIT :
Okay i'll make it simple, here is the complete workspace of the project :
http://www.lecompteestbon.com/Android.LCEB.zip
What i want to do is an offline version of the website lecompteestbon, which allows people to do accounting between friends after a weekend.
TabPeople = Add the list of friends TabTransactions = List of expenses TabTransaction = Add an expense TabResult = Calculate the list of payments
Let me know how to make this work :)
推荐答案
What's about setting a static field into the myTabs object?
public class myTabs extends TabActivity { .... public static arrayPeople = new ArrayList<String>(); ....
On TabOne.java:
@Override public void onStop(){ myTabs.arrayPeople = arrayPeople; }
On TabTwo.java:
arrayPeople = myTabs.arrayPeople
Does make sense?
其他推荐答案
With the examples above, it was really work when I replace the "onStop" = "onPause"
/** Called when the activity looses focus **/ @Override public void onStop() { Intent myIntent = new Intent(); myIntent.putStringArrayListExtra("arrayPeople", arrayPeople); this.setIntent(myIntent); } /** Called when the activity looses focus **/ @Override public void onPause() { Intent myIntent = new Intent(); myIntent.putStringArrayListExtra("arrayPeople", arrayPeople); this.setIntent(myIntent); }
其他推荐答案
I don't think altering the intent in onStop() will work because the TabHost handles the intents. Setting one class' intent with this.setIntent() wont let another class access it with getIntent() as they are built on different intents.
I would either store my arrayPeople in a database or I would pass arrayPeople back to the TabHost with MyTabs.setArrayPeople or something similar. Then you can query the db onCreate of your next tab or just pull it from your MyTabs Class.