简体   繁体   中英

Passing EditText Value from an Activity to a Tab in Android Tabhost

I have tabhost contains two tabs (each is an activity): "Add New Data" and "My Data". The default tab shown first is the "Add New Data" since it is the #1 tab.

To access both tabs, I have to be logged in first. What I want to achieve is I want to pass the value of "Username" textfield in Login activity onto the "My Data" (2nd tab) tab. I'm quite bewildered since I have to click the tab "My Data" to open it and I have no idea on how to pass a textfield value from a Login class to the tab ("My Data").

If you have any suggestion on how to achieve it, please let me know.

I've searched through the forum but didnt find any suitable solution. Here is my code in the tabhost class (Dashboard.java):

public class Dashboard extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    TabHost host = getTabHost();
    host.addTab(host.newTabSpec("one").setIndicator("ADD NEW DATA")
            .setContent(new Intent(this, Input_form.class)));
    host.addTab(host.newTabSpec("two").setIndicator("MY DATA")
            .setContent(new Intent(this, MyData.class)));
}

// On Back Button Pressed

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        startActivity(new Intent(this, HomeLogin.class));           
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

Is there anything I could modify in the code above to achieve what I want? If yes please tell me, or if it is not through this code, please let me know Thank you

Finally after trying while reading several tutorials and examples, I've come to a solution for this problem. I am going to share it here so those who have similar problem can overcome it.

The trick is I don't need to pass the textfield value (which was intended to be the username). I just need to select the id of the user in database through Login class and pass it through an intent to the tabhost class. To pass the id to the tabs, I did it by using putExtra in Login class like this:

Intent passId = new Intent(getApplicationContext(), Dashboard.class);
passId.putExtra(ID_USER, iduser);
startActivity(passId);

And here is the secret, to catch the Extra, I did it like this in my tabhost class (I read it from here: How to pass a parameter to a new activity with tab host )

Here is my code in the body of tabhost class (Dashboard.java):

    //passing id to tab #1
    Intent passing1 = new Intent();
    Bundle bundle = getIntent().getExtras();
    String id=bundle.get(ID_USER).toString();   
    passing1.putExtra("iduser", id);
    passing1.setClass(this, Input_form.class);

    TabHost host = getTabHost();
    host.addTab(host.newTabSpec("one").setIndicator("ADD NEW DATA")
            .setContent(passing1));

    //passing idkont to tab #2

    Intent passing2 = new Intent();
    Bundle bundle2=getIntent().getExtras();
    String id2 = bundle2.get(ID_USER).toString();
    passing2.putExtra("iduser", id2);
    passing2.setClass(this, MyData.class);



    host.addTab(host.newTabSpec("two").setIndicator("MY DATA")
            .setContent(passing2));

Then, in the activity class of both tab, I just need to catch the extra like usual using getIntent and getStringExtra. After getting the extra in both class, finally now I can use the data/variable that is passed to the tab to do more CRUD in my app.

Hope it helps, Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM