简体   繁体   English

将数据从“扩展活动”的类传递到“扩展片段”的类

[英]Passing data from a class that 'extends activity' to a class that 'extends fragment'

What i'm trying to do is to send information obtained in a class that 'extends activity' to a class that 'extends fragment' where this 'extends fragment' is part of a 'PagerAdapter' 我想做的是将在“扩展活动”类中获得的信息发送到“扩展片段”的类中,其中“扩展片段”是“ PagerAdapter”的一部分

My initial thought was to pass the information through an intent to the class that 'extends fragment activity' and then pass the information to the appropriate fragment using 'getActivity().getIntent.getExtras()' but this doesn't seem to work, as the information has to be displayed in a textView. 我最初的想法是将信息通过意图传递给“扩展片段活动”的类,然后使用“ getActivity()。getIntent.getExtras()”将信息传递给适当的片段,但这似乎不起作用,因为信息必须显示在textView中。

My classes are displayed below.. 我的课程显示在下面。

ObtainUserInformation.java ObtainUserInformation.java

public class ObtainUserInformation extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.obtain_user_information);
}

public void submitInformation(View view){
    EditText maintenanceTxt = (EditText)findViewById(R.id.obUserInfo_maintenence);
    EditText grantTxt = (EditText)findViewById(R.id.obUserInfo_grant);
    EditText bursaryTxt = (EditText)findViewById(R.id.obUserInfo_bursary);
    EditText barclaysTxt = (EditText)findViewById(R.id.obUserInfo_barclays);
    EditText natwestTxt = (EditText)findViewById(R.id.obUserInfo_natwest);
    EditText hsbcTxt = (EditText)findViewById(R.id.obUserInfo_hsbc);
    EditText walletTxt = (EditText)findViewById(R.id.obUserInfo_wallet);

    String maintenance = maintenanceTxt.getText().toString();
    String grant = grantTxt.getText().toString();
    String bursary = bursaryTxt.getText().toString();
    String barclays = barclaysTxt.getText().toString();
    String natwest = natwestTxt.getText().toString();
    String hsbc = hsbcTxt.getText().toString();
    String wallet = walletTxt.getText().toString();

    if(maintenance.isEmpty()|| grant.isEmpty() || bursary.isEmpty() || barclays.isEmpty() || natwest.isEmpty()
            || hsbc.isEmpty() || wallet.isEmpty()){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Empty textbox!"); 
        builder.setMessage("Please enter 0.00 for any empty textboxes"); builder.setPositiveButton("OK", null);
        @SuppressWarnings("unused")
        AlertDialog dialog = builder.show();
    }
    else{
        float convMaintenance = Float.valueOf(maintenance);
        float convGrant = Float.valueOf(grant);
        float convBursary = Float.valueOf(bursary);
        float convBarclays = Float.valueOf(barclays);
        float convNatwest = Float.valueOf(natwest);
        float convHsbc = Float.valueOf(hsbc);
        float convWallet = Float.valueOf(wallet);

        Intent intent = new Intent(ObtainUserInformation.this, MainMenu.class);
        intent.getFloatExtra("maintenance", convMaintenance);
        intent.getFloatExtra("grant", convGrant);
        intent.getFloatExtra("bursary", convBursary);
        intent.getFloatExtra("barclays", convBarclays);
        intent.getFloatExtra("natwest", convNatwest);
        intent.getFloatExtra("hsbc", convHsbc);
        intent.getFloatExtra("wallet", convWallet);
        Toast.makeText(ObtainUserInformation.this, "Financial information successfully saved!", Toast.LENGTH_LONG).show();
        startActivity(intent);
        finish();
    }
}

} }

What this class is doing is to grab the users input on an EditText, put them into variables, and send these variables through an intent. 此类的作用是获取用户在EditText上的输入,将其放入变量中,然后通过意图发送这些变量。

MainMenu.java MainMenu.java

public class MainMenu extends FragmentActivity{

DBAdapter db = new DBAdapter(this); //Initiate DB class methods
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

//OnCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);   
}

**UNNCESSARY CODE OMMITED**

public void getUserInformation() {
    float maintenance = getIntent().getExtras().getFloat("maintenance");
    float grant = getIntent().getExtras().getFloat("grant");
    float bursary = getIntent().getExtras().getFloat("bursary");
    float barclays = getIntent().getExtras().getFloat("barclays");
    float natwest = getIntent().getExtras().getFloat("natwest");
    float hsbc = getIntent().getExtras().getFloat("hsbc");
    float wallet = getIntent().getExtras().getFloat("wallet");
    EditText maintenanceTxt = (EditText)findViewById(R.id.fragment3_maintenance);
    EditText grantTxt = (EditText)findViewById(R.id.fragment3_grant);
    EditText bursaryTxt = (EditText)findViewById(R.id.fragment3_bursary);
    EditText barclaysTxt = (EditText)findViewById(R.id.fragment3_barclays);
    EditText natwestTxt = (EditText)findViewById(R.id.fragment3_natwest);
    EditText hsbcTxt = (EditText)findViewById(R.id.fragment3_hsbc);
    EditText walletTxt = (EditText)findViewById(R.id.fragment3_wallet);
    maintenanceTxt.setTextSize(maintenance);
    grantTxt.setTextSize(grant);
    bursaryTxt.setTextSize(bursary);
    barclaysTxt.setTextSize(barclays);
    natwestTxt.setTextSize(natwest);
    hsbcTxt.setTextSize(hsbc);
    walletTxt.setTextSize(wallet);
}

MORE UNNECESSARY CODE OMITTED

What this class contains is a method 'getUserInformation' that grabs the intent, finds the EditText id and sets the intent variable to this text field. 此类包含的是方法“ getUserInformation”,该方法可获取意图,找到EditText id并将意图变量设置为此文本字段。

Fragment3 片段3

public class Fragment3 extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment3, container, false);

    //Set date to TextView
    long date = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy");
    String dateString = sdf.format(date);
    TextView t = (TextView) root.findViewById(R.id.textView_date3);
    t.setText(dateString);
    return root;
}

This class is where I want the variables to be displayed in EditText boxes but struggling to find a suitable method to complete this task. 我想在此类中将变量显示在EditText框中,但努力寻找合适的方法来完成此任务。

It becomes more difficult when working with fragments instead of ordinary activity classes... 当使用片段而不是普通的活动类时,这变得更加困难。

Just a break down of what I want to happen is: 1.) User enters information to a 1 time activity 2.) This information is displayed in a fragment that's part of a pagerAdapter 我要发生的事情只是一个分解:1.)用户输入一次活动的信息2.)此信息显示在pagerAdapter的一部分中

Any help would be much appreciated! 任何帮助将非常感激!

Use Bundle like this if you want to pass data while creating the Fragment. 如果要在创建片段时传递数据,请像这样使用Bundle。

Bundle args = new Bundle();
args.putInt("yourStringName", yourSting);
fragment.setArguments(args);

If you want to contact the enclosing activity from fragment. 如果要联系片段中的封闭活动。 You can get the Activity instance using getActivity method and call the function you wanted to call. 您可以使用getActivity方法获取Activity实例,然后调用要调用的函数。 In your case even you can access the Intent associated with the FragmentActivity and access the extra values from it. 就您而言,您甚至可以访问与FragmentActivity相关联的Intent并从中访问其他值。

public void putFloat (String key, float value) public void putFloat(字符串键,浮点值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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