2011-09-07 2 views
1

나는 응용 프로그램을 만들고 있는데 응용 프로그램의 주요 활동에는 6 개의 이미지 단추가 있습니다. 주 활동 코드는하나의 활동으로 ArrayList를 수집하는 방법

패키지 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; 
    } 
} 

라디오 버튼을 클릭하면 새로운 활동이 열립니다. 활동 중 하나에 대한 코드 나 6 개 활동이

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

이다 ... 나 ... 각 활동에서 배열 ArrayList를 통과하고 그것 MainActivity의 ArrayList를 수집하기 위해 다른리스트를 최종적으로 통과해야 수업.

모든 활동에서이 목록을 전달하고 기본 활동에서 수집하는 방법은 무엇입니까? 어쩌면 작동하고 있음을 시도함으로써

+0

와 동일한 기능을 수행 할 것 ... 조금 느렸다 검색하고 글로벌 빠르게 구현했다 설정 때문에 내 bluetoothAdapter으로 이런 짓을? ? – confucius

+0

예 .. 그들은 MainActivity에있는 Arraylist에 추가되어야합니다. – android

답변

1

, 당신은 응용 프로그램 클래스의 홀더의 ArrayList를 배치 할 수 있습니다 및 getter와 setter를 제공하여 시스템이 전역 적으로 활용할 수 있도록합니다. 한 번 활동에서 다른 활동으로 이동해야하는 경우 번들에 배치하는 것이 좋습니다.

0

: 당신은 당신이 시스템 전체에 접근해야 할 ArrayList에있는 경우

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

보다도,

0

나는 각 활동에서 arraylist를 빠져 나간다. 이것은 내가 발견 한 바있는 기억 문제로 이어질 수있다. 더 좋은 방법 중 하나는 모든 활동에서 볼 수있는 글로벌 목록을 만드는 것입니다. 나는 당신의 배열 목록 당신이 시작 각 활동에서의 ArrayList를 보낼

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

} 
관련 문제