2011-09-07 28 views
1

我在制作应用程序,应用程序的主要活动有6个图像按钮。 主要活动的代码是如何在单个活动中收集ArrayList

package org.personal.reader;

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

我有6个活动...我需要从每个活动传递数组ArrayList和收集它在MainActivity一个的ArrayList ...最后通该列表到另一个类。

如何从所有活动中传递此列表并将其收集到主要活动中?

+0

? ? – confucius

+0

是的..他们应该被添加到MainActivity中的Arraylist中 – android

回答

1

如果您希望在整个系统中访问一个ArrayList,您可以放置​​一个支架的ArrayList的应用类别及提供getter和setter,以便他们的系统可以在全球范围内使用它。如果它只需要从一个活动转移到另一个活动,我会建议将其放入捆绑包中。

0

通过努力,也许它是工作:

 Bundle bundle = new Bundle(); 
     bundle.putStringArrayList("DONE", activeURL1); 
     Intent myIntent = new Intent(Reader.this,MainActivity.class); 
     myIntent.**putStringArrayListExtra**("reader", activeURL1); 
     startActivity(myIntent); 

最佳,

0

我会远离从每个活动传递一个数组列表,因为这会导致一些内存问题,因为我发现了。更好的方法之一是创建所有活动都可以看到的全局列表。我这样做是我的bluetoothAdapter因为检索,这是一个有点慢,并设置其全球为较快的实现......我会做同样的你的数组列表

public class MyStates extends Application { 

    /** The bluetooth adapter for the phone **/ 
    private BluetoothAdapter blueToothAdapter; 

    public BluetoothAdapter getBtState() { 
     return this.blueToothAdapter; 
    } 

    public void setBtState(BluetoothAdapter cust) { 
     this.blueToothAdapter = cust; 
    } 

} 
要从每次启动活动发的ArrayList
相关问题