0

私は4つのアクティビティを持っています。値をTABアクティビティに移す

最初のアクティビティはデータ入力に使用され、2番目のアクティビティはTABメインアクティビティ(拡張TabActivity)で各タブアクティビティが宣言され、3番目と4番目のアクティビティはタブアクティビティです。

最初のアクティビティから3番目と4番目のアクティビティに値を転送するにはどうすればよいですか?

は、ここに私の第一の活動です:

public class FirstActivity extends Activity { 
    EditText inputName; 
    EditText inputAddress; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //declaration layout 
    inputName = (EditText) findViewById(R.id.editText1); 
    inputAddress = (EditText) findViewById(R.id.editText2); 
    Button btnNextScreen = (Button) findViewById(R.id.button1); 

    //Listening to button event 
    btnNextScreen.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View arg0) { 
     //Starting a new Intent 
     Intent nextScreen = new Intent(getApplicationContext(), MainTabActivity.class); 


     //Sending data to another Activity 
     //THIS IS THE VALUE I WANNA TRANSFER TO TAB ACTIVITY 
     nextScreen.putExtra("name", inputName.getText().toString()); 
     nextScreen.putExtra("address", inputEmail.getText().toString()); 

     // starting new activity 
     startActivity(nextScreen); 

     } 
    }); 
    } 
} 

とここに私のMainTabActivityです:

public class MainTabActivity extends TabActivity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.maintabresto); 


    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable 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, InfoTab.class); 
    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("info").setIndicator("Info", 
         res.getDrawable(R.drawable.iconinfotab)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, MenuTab.class); 
    spec = tabHost.newTabSpec("menu").setIndicator("Menu", 
         res.getDrawable(R.drawable.iconmenutab)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(2); 
    } 
} 

、ここで私の第三第四の活動です(私はそれぞれの活動でのTextViewする前に転送する値を表示したいです):

public class InfoTab extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.infotab); 

    TextView txtname = (TextView) findViewById(R.id.textTEST1); 
    TextView txtaddress = (TextView) findViewById(R.id.TextTEST2); 


    //displaying data from previous activity 
    //this is didnt work 
    Intent i = getIntent(); 
    // Receiving the Data 
    String name = i.getStringExtra("name"); 
    String address = i.getStringExtra("address"); 

    // Displaying Received data 
    //this is didnt work 
    txtname.setText(name); 
    txtaddress.setText(address); 
    } 
} 

誰でも私を助けることができますか? 私はfirts活動にこれを実行しようとしました:

Intent nextScreen = new Intent(getApplicationContext(), InfoTab.class); 

それが動作する(値がnextactivityに転送、代わりにタブのアクティビティを開いて、活動は(ないタブに個別に開くことができます)。..! 。

はので、ここで私の目標は、各タブの活動に値を転送することであるAN彼らはMainTabActivityの下に開いている。

申し訳ありませんが、私はいくつかの小さなミスではあると思うならば私の英語イマイチ良い。

+0

誰でも私を助けることができますか? –

+0

解決しましたか?私も問題に直面しています。 –

答えて

0

あなたのコードですが、私は理解できませんそれを出す。

以下は、私があなたが望んだのと同じことをしている私のtabActivityのコードです。

public class MyTabActivity extends TabActivity { 
    TabHost myTabHost; 
    String data1, data2, data3; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_tab_activity); 

     Intent i = getIntent(); 
     data1 = i.getStringExtra("Data1"); 
     data2 = i.getStringExtra("Data2"); 
     data3 = i.getStringExtra("Data3"); 

     setTabs(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.my_tab_activity, menu); 
     return true; 
    } 

    public void setTabs() { 
     myTabHost = getTabHost(); 
     addTab(R.string.tab1Txt, R.drawable.video); 
     addTab(R.string.tab2Txt, R.drawable.pdf); 
     addTab(R.string.tab3Txt, R.drawable.notepad); 
     myTabHost.setCurrentTab(2); 
    } 

    public void addTab(int labelId, int drawableId) { 
     Intent tabIntent = null; 
     TabHost.TabSpec spec; 
     switch (labelId) { 
     case R.string.tab1Txt: 
      tabIntent = new Intent(this, Tab1Activity.class); 
      tabIntent.putExtra("Data", data1); 
      break; 
     case R.string.tab2Txt: 
      tabIntent = new Intent(this, Tab2Activity.class); 
      tabIntent.putExtra("Data", data2); 
      break; 
     case R.string.tab3Txt: 
      tabIntent = new Intent(this, Tab3Activity.class); 
      tabIntent.putExtra("Data", data3); 
      break; 
     } 

     spec = myTabHost.newTabSpec("tab" + labelId); 
     View tabIndicator = LayoutInflater.from(this).inflate(
       R.layout.indicator, getTabWidget(), false); 

     TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
     title.setText(labelId); 
     title.setTextColor(Color.BLUE); 
     ImageView img = (ImageView) tabIndicator.findViewById(R.id.icon); 
     img.setImageResource(drawableId); 
     spec.setIndicator(tabIndicator); 
     spec.setContent(tabIntent); 
     myTabHost.addTab(spec); 
    } 
} 

希望します。

関連する問題