2012-01-10 44 views
0

我有一個帶5個按鈕的主屏幕。每次按一個按鈕我想要一個新的屏幕。到目前爲止,我還有其他5個類(每個屏幕)和5 xmls。但Iam肯定會有更好的方式,因爲這個屏幕和xml是相同的,我想要做的是改變一些文本和一些數據從數據庫中獲取。我相信我可以繼續使用另一個類,只有一個xml,然後傳遞我想要的值作爲參數。 (想象一下,在最終狀態下,我的應用程序必須有15個按鈕,所以我認爲這太浪費空間了,並且沒有必要擁有15個.java文件和15個xml文件,這些文件看起來是一樣的,只有圖像和文本視圖的某些值會發生變化) 。我的主要活動代碼:讓我的課變成一個參數

public class MyActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    main2Theme();} 

private void main2Theme() { 
    setContentView(R.layout.main_new); 

    Button Button100 = (Button)findViewById(R.id.Button100); 
Button113.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      Intent i = new Intent(this, OtherScreenName.class); 
      startActivity(i); 
     } 
    }); //and so on for 15 buttons 

我OtherScreenName.class是:

public class OtherScreenName extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Theme(); 
} 

@SuppressWarnings("null") 
    private void Theme() { 
     Log.d("SnowReportApp","Do first thing"); 
     setContentView(R.layout.otherscreenname); //THIS CHANGES IN EVERY CLASS DEPENDING ON THE ID OF THE BUTTON 
     String result = ""; 
     InputStream is = null; 
     StringBuilder sb=null; 

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before 
     nameValuePairs.add(new BasicNameValuePair("ski_id","OtherScreenName")); 
     TextView text1 = (TextView) findViewById(R.id.otherscreenname_1); 
//THESE 2 CHANGE DEPERNDING ON THE BUTTON PRESSED 
//perform action..... 

//AND ALSO HERE I NEED THE ID OF THE BUTTON 

b1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent i = new Intent(OtherScreenName.this,MyActivity.class); 
       startActivity(i); 

     } 
     }); 

任何人都可以提出如何給參數上我的課,什麼應該是他們的類型?

回答

1

如果我正確理解你的問題,你想傳遞參數給活動。通常它會通過類的構造函數完成,但是,活動不能有用戶定義的構造函數,只有默認的構造函數;它們只能通過意圖創建間接實例化。

bundle.putInt("intName",intValue); 

,那麼你可以在開始活動之前

int intValue = bundle.getInt("intName"); 

認沽額外提取包中的數據:如果你需要傳遞活動之間的數據,通過把羣衆演員束,例如做:

Intent i = new Intent(this, YourActivity.class); 
Bundle b = new Bundle(); 

b.putInt("intName",intValue); 
i.putExtras(b); 
startActivity(i); 

,然後讀取在onCreate方法的額外:

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    Bundle b = getIntent().getExtras(); 
    int intValue; 

    if (b != null) 
    { 
     intValue= b.getInt("intName"); 
    } 
} 

與傳遞其他數據類型的方式相同,如字符串布爾等。如果這不夠,並且您需要傳遞一些更復雜的數據,則使用Parcelable interface

+0

就是這樣。因此,儘管我想傳遞的類型的數量和參數,我創建了一個包,然後說b.putInt,b.putInt作爲第二個包,以此類推? (或每個參數不同的捆綁包?)非常感謝! – ghostrider 2012-01-10 22:06:57

+1

是的,確切地說。當然,如果你有很多想要傳遞的參數,比將它們包裝到實現Parcelable接口的類中並將它放到Extras中'b.putExtra(String name,Parcelable value)' – vitakot 2012-01-10 22:22:43

1

您可以通過添加額外的將其與意圖傳遞參數,像下面這樣:

Intent i = new Intent(this, MyActivity.class); 
i.putExtra("paramName", "value"); 
startActivity(i); 

在你的活動,你可以使用getIntent()方法來檢索意圖和提取您的參數(S )從它:

Intent i = getIntent(); 
Bundle extras = i.getExtras(); 
String param = extras.getString("paramName", "default value"); 

您可以將所有不同的文本和數據在你的意圖,但你也可以基於一個Intent參數數據和文本檢索其值決定。如果它是很多數據或文本,那麼使用第二種方法可能會更好。

1
If you want to pass object instead of simple data, you must use parcelable objects as it´s explained in: [http://developer.android.com/reference/android/os/Parcelable.html][1] 

Reading and writing with parcelable objects is very similar to the way with a simple data 

Intent intent = new Intent(this ,newthing.class); 
Bundle b = new Bundle(); 
b.putParcelable("results", listOfResults); 
intent.putExtras(b); 
startActivityForResult(intent,0); 

Intent i = getIntent(); 
Bundle extras = i.getExtras(); 
String param = extras.getParcelable("results"); 
相關問題