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

时间:2014-02-15 16:55:45

标签: java android eclipse android-intent fragment

我要做的是将“扩展活动”类中获得的信息发送到“扩展片段”的类,其中“扩展片段”是“PagerAdapter”的一部分

我最初的想法是通过意图将信息传递给'扩展片段活动'的类,然后使用'getActivity()。getIntent.getExtras()'将信息传递给适当的片段,但这似乎不是工作,因为信息必须在textView中显示。

我的课程显示在下面..

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

}

这个类正在做的是抓取EditText上的用户输入,将它们放入变量,并通过intent发送这些变量。

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

此类包含的是一个方法'getUserInformation',用于获取intent,查找EditText id并将intent变量设置为此文本字段。

Fragment3

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

这个类是我希望变量显示在EditText框中但很难找到合适的方法来完成此任务的地方。

使用片段而不是普通的活动类时会变得更加困难......

只是打破了我想要发生的事情: 1.)用户输入1次活动的信息 2.)此信息显示在pagerAdapter

的一部分中

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

如果要在创建片段时传递数据,请使用此类Bundle。

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

如果要从片段联系封闭活动。您可以使用Activity方法获取getActivity实例,并调用您想要调用的函数。在您的情况下,即使您可以访问与FragmentActivity关联的Intent并从中访问其他值。

public void putFloat (String key, float value)