Android 如何在单个活动中收集ArrayList
我正在制作一个应用程序,该应用程序的主要活动有6个图像按钮。 主要活动的代码是 包org.personal.readerAndroid 如何在单个活动中收集ArrayList,android,Android,我正在制作一个应用程序,该应用程序的主要活动有6个图像按钮。 主要活动的代码是 包org.personal.reader public class MainActivity extends Activity implements Button.OnClickListener { ArrayList<String> activeURL = new ArrayList<String>(); private Button Done, Refresh;
public class MainActivity extends Activity implements
Button.OnClickListener {
ArrayList<String> activeURL = new ArrayList<String>();
private Button Done, Refresh;
int i=0;
private ImageButton B1, B2, B3, B4, B5, B6;
public static final String LOG_TAG = "Main Activity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Done = (Button)findViewById(R.id.DONE);
Done.setOnClickListener(this);
Refresh = (Button)findViewById(R.id.REFRESH);
Refresh.setOnClickListener(this);
B1 = (ImageButton)findViewById(R.id.news1);
B1.setOnClickListener(this);
B2 = (ImageButton)findViewById(R.id.sports1);
B2.setOnClickListener(this);
B3 = (ImageButton)findViewById(R.id.weather1);
B3.setOnClickListener(this);
B4 = (ImageButton)findViewById(R.id.entertainment1);
B4.setOnClickListener(this);
B5 = (ImageButton)findViewById(R.id.health1);
B5.setOnClickListener(this);
B6 = (ImageButton)findViewById(R.id.tech1);
B6.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if ( v==B1)
{
Intent intent = new Intent(MainActivity.this, Reader.class);
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
if ( v==B2 )
{
Intent intent = new Intent(MainActivity.this, Reader2.class);
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
if ( v==B3 )
{
Intent intent = new Intent(MainActivity.this, Reader3.class);
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
if ( v==B4 )
{
Intent intent = new Intent(MainActivity.this, Reader4.class);
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
if ( v==B5 )
{
Intent intent = new Intent(MainActivity.this, Reader5.class);
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
if ( v==B6 )
{
Intent intent = new Intent(MainActivity.this, Reader6.class);
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
if (v==Done)
{ ArrayList<String> array =add();
Bundle bundle = new Bundle();
bundle.putStringArrayList("DONE", array);
Intent myIntent = new Intent(MainActivity.this,Aggregator.class);
myIntent.putExtra("agg", array);
startActivity(myIntent);
}
}
public ArrayList<String> add()
{
Bundle bundle=this.getIntent().getExtras();
ArrayList<String> temp = bundle.getStringArrayList("reader");
ArrayList<String> temp1 = new ArrayList<String>();
temp1=temp;
if (temp1!=null)
activeURL.addAll(temp1);
return activeURL;
}
}
public类MainActivity扩展活动实现
Button.OnClickListener{
ArrayList activeURL=新的ArrayList();
私有按钮完成,刷新;
int i=0;
专用图像按钮B1、B2、B3、B4、B5、B6;
public static final String LOG_TAG=“Main Activity”;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
完成=(按钮)findViewById(R.id.Done);
完成。setOnClickListener(此);
刷新=(按钮)findViewById(R.id.Refresh);
Refresh.setOnClickListener(这个);
B1=(ImageButton)findViewById(R.id.news1);
B1.setOnClickListener(本);
B2=(图像按钮)findViewById(R.id.1);
B2.setOnClickListener(本);
B3=(图像按钮)findViewById(R.id.weather1);
B3.setOnClickListener(本);
B4=(图像按钮)findViewById(R.id.Entertment1);
B4.setOnClickListener(本);
B5=(图像按钮)findViewById(R.id.health1);
B5.setOnClickListener(本);
B6=(ImageButton)findViewById(R.id.tech1);
B6.setOnClickListener(本);
}
@凌驾
公共void onClick(视图v){
如果(v==B1)
{
意图=新意图(MainActivity.this,Reader.class);
intent.setAction(intent.ACTION_MAIN);
星触觉(意向);
}
如果(v==B2)
{
意向意向=新意向(MainActivity.this,Reader2.class);
intent.setAction(intent.ACTION_MAIN);
星触觉(意向);
}
如果(v==B3)
{
意向意向=新意向(MainActivity.this,Reader3.class);
intent.setAction(intent.ACTION_MAIN);
星触觉(意向);
}
如果(v==B4)
{
意向意向=新意向(MainActivity.this,Reader4.class);
intent.setAction(intent.ACTION_MAIN);
星触觉(意向);
}
如果(v==B5)
{
意向意向=新意向(MainActivity.this,Reader5.class);
intent.setAction(intent.ACTION_MAIN);
星触觉(意向);
}
如果(v==B6)
{
意向意向=新意向(MainActivity.this,Reader6.class);
intent.setAction(intent.ACTION_MAIN);
星触觉(意向);
}
如果(v==完成)
{ArrayList array=add();
Bundle=新Bundle();
bundle.putStringArrayList(“完成”,数组);
Intent myIntent=新的Intent(MainActivity.this、Aggregator.class);
myIntent.putExtra(“agg”,数组);
星触觉(myIntent);
}
}
公共ArrayList add()
{
Bundle Bundle=this.getIntent().getExtras();
ArrayList temp=bundle.getStringArrayList(“读卡器”);
ArrayList temp1=新的ArrayList();
temp1=temp;
if(temp1!=null)
addAll(temp1);
返回activeURL;
}
}
单击任何单选按钮,将打开一个新活动。
其中一个活动的代码为
public class Reader extends Activity implements
Button.OnClickListener {
/** Called when the activity is first created. */
private RadioButton RB1, RB2, RB3, RB4, RB5;
private Button Done;
ArrayList<String> activeURL1 = new ArrayList<String>();
public static final String LOG_TAG = "Activity 1";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
RB1 = (RadioButton) findViewById(R.id.BBC);
RB2 = (RadioButton) findViewById(R.id.Guardian);
RB3 = (RadioButton) findViewById(R.id.msnbc);
RB4 = (RadioButton) findViewById(R.id.Sky);
RB5 = (RadioButton) findViewById(R.id.FOX);
Done = (Button) findViewById(R.id.DONE);
Done.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String[] array;
array = new String[5];
array[0] = "http://feeds.bbci.co.uk/news/world/rss.xml";
array[1] = "http://feeds.guardian.co.uk/theguardian/rss";
array[2] = "http://rss.msnbc.msn.com/id/3032506/device/rss/rss.xml";
array[3] = "http://news.sky.com/sky-news/rss/world-news/rss.xml";
array[4] = "http://www.foxnews.com/about/rss/feedburner/foxnews/most-popular";
if (RB1.isChecked() == true)
activeURL1.add(array[0]);
if (RB2.isChecked() == true)
activeURL1.add(array[1]);
if (RB3.isChecked() == true)
activeURL1.add(array[2]);
if (RB4.isChecked() == true)
activeURL1.add(array[3]);
if (RB5.isChecked() == true)
activeURL1.add(array[4]);
if(v==Done)
{
Bundle bundle = new Bundle();
bundle.putStringArrayList("DONE", activeURL1);
Intent myIntent = new Intent(Reader.this,MainActivity.class);
myIntent.putExtra("reader", activeURL1);
startActivity(myIntent);
}
}
}
公共类读取器扩展活动实现
Button.OnClickListener{
/**在首次创建活动时调用*/
专用单选按钮RB1、RB2、RB3、RB4、RB5;
私人按钮完成;
ArrayList activeURL1=新的ArrayList();
公共静态最终字符串日志\u TAG=“活动1”;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
RB1=(单选按钮)findViewById(R.id.BBC);
RB2=(单选按钮)findViewById(R.id.Guardian);
RB3=(单选按钮)findViewById(R.id.msnbc);
RB4=(单选按钮)findViewById(R.id.Sky);
RB5=(单选按钮)findViewById(R.id.FOX);
完成=(按钮)findViewById(R.id.Done);
完成。setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
字符串[]数组;
数组=新字符串[5];
数组[0]=”http://feeds.bbci.co.uk/news/world/rss.xml";
数组[1]=”http://feeds.guardian.co.uk/theguardian/rss";
数组[2]=”http://rss.msnbc.msn.com/id/3032506/device/rss/rss.xml";
数组[3]=”http://news.sky.com/sky-news/rss/world-news/rss.xml";
数组[4]=”http://www.foxnews.com/about/rss/feedburner/foxnews/most-popular";
if(RB1.isChecked()==true)
activeURL1.add(数组[0]);
if(RB2.isChecked()==true)
activeURL1.add(数组[1]);
if(RB3.isChecked()==true)
activeURL1.add(数组[2]);
if(RB4.isChecked()==true)
activeURL1.add(数组[3]);
if(RB5.isChecked()==true)
activeURL1.add(数组[4]);
如果(v==完成)
{
Bundle=新Bundle();
bundle.putStringArrayList(“完成”,activeURL1);
Intent myIntent=newintent(Reader.this,MainActivity.class);
myIntent.putExtra(“阅读器”,activeURL1);
星触觉(myIntent);
}
}
}
我有6项活动。。。我需要从每个活动中传递一个数组Arraylist,并在MainActivity中收集一个Arraylist。。。最后,将此列表传递给另一个类
如何从所有活动中传递此列表并将其收集到主活动中?通过尝试它可能正在工作:
Bundle bundle = new Bundle();
bundle.putStringArrayList("DONE", activeURL1);
Intent myIntent = new Intent(Reader.this,MainActivity.class);
myIntent.**putStringArrayListExtra**("reader", activeURL1);
startActivity(myIntent);
最好的方法是,尝试一下它是否有效:
Bundle bundle = new Bundle();
bundle.putStringArrayList("DONE", activeURL1);
Intent myIntent = new Intent(Reader.this,MainActivity.class);
myIntent.**putStringArrayListExtra**("reader", activeURL1);
startActivity(myIntent);
最好是,如果您希望在整个系统中访问一个arraylist,您可以在应用程序中放置一个holder arraylist