How to pass the previous data using spinner and image? - java

I have problem with my android coding. I want to make an update process. But first of all, I'm trying to collect all the data from edit text, spinner and image. But I have problem with spinner and image, could not get the previous data. How to get the previous data using spinner and image? Ex.. Image1
shows the complete retrieve process when user click on Submit Button, then it will go to this View Activity interface. While in Image2
shows the Edit Activity interface where the user can update all the data from there when the user click on Edit Button. The issue now is, I can not collect the previous data using spinner and image for editing/updating purposes as shown in the Image 2. Really hope someone can help me.. Thanks in advance...
Coding as follows:
1) Coding from View Activity:-
EditAdsButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent editAds = new Intent(ViewAdsActivity.this, EditAdsActivity.class);
startActivity(editAds);
String tn = ViewTuitionName.getText().toString();
String pn = ViewProviderName.getText().toString();
String pg = getIntent().getStringExtra("PG");
Intent i = new Intent(ViewAdsActivity.this, EditAdsActivity.class);
i.putExtra("TN", "" +tn); // Collected from EditText or any other source
i.putExtra("PN", "" +pn);
i.putExtra("PG", "" +pg);
startActivity(i);
}
});
2) Coding from Edit Activity (onCreate method):-
Intent i = getIntent();
String tn = i.getStringExtra("TN");
String pn = i.getStringExtra("PN");
String pg = getIntent().getStringExtra("PG");
EditTuitionName.setText(tn);
EditProviderName.setText(pn);
EditProviderGender.setSelection(pg);

Here is a solution:
Constant.java
public class Constant {
public static final String[] GENDER = {"Male", "Female", "Other"};
}
ViewAdsActivity.java
public class ViewAdsActivity extends AppCompatActivity {
private CircleImageView avatar;
private EditText name;
private Spinner gender;
private Button next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_ads);
avatar = findViewById(R.id.avatar);
name = findViewById(R.id.edit_text_name);
gender = findViewById(R.id.spinner_gender);
next = findViewById(R.id.button_next);
// Render avatar
String imageUrl = "https://vignette.wikia.nocookie.net/spiritedaway/images/6/69/Chihiro.jpg/revision/latest?cb=20170308090934";
avatar.setTag(imageUrl);
Picasso.get()
.load(imageUrl)
.into(avatar);
// Render spinner
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Constant.GENDER);
gender.setAdapter(adapter);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ViewAdsActivity.this, EditAdsActivity.class);
intent.putExtra("imageUrl", (String) avatar.getTag());
intent.putExtra("genderPosition", gender.getSelectedItemPosition());
intent.putExtra("name", name.getText().toString());
startActivity(intent);
}
});
}
}
EditAdsActivity.java
public class EditAdsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_ads);
CircleImageView avatar = findViewById(R.id.avatar);
EditText nameEditText = findViewById(R.id.edit_text_name);
Spinner gender = findViewById(R.id.spinner_gender);
String imageUrl = getIntent().getStringExtra("imageUrl");
int genderPosition = getIntent().getIntExtra("genderPosition", 0);
String name = getIntent().getStringExtra("name");
// Render image view
Picasso.get()
.load(imageUrl)
.into(avatar);
// Render spinner
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Constant.GENDER);
gender.setAdapter(adapter);
gender.setSelection(genderPosition);
// Render edit text
nameEditText.setText(name);
}
}

For the spinner you can use
Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
And then place the text in the intent like the other objects.
An image is trickier because you don't want to serialize the bitmap for the intent since serialization is on the main thread and the app would probably stutter or hang a bit. Instead you could store the bitmap in a class that is accessible from both activities, or if the image is already stored on the device you can pass the URI and just reopen it in the next activity.

Related

How do I pass a command to the component of another activity?

I have two activities. When an item in a RecyclerView is selected, it takes the user to the second activity and fills in the details with the related RecyclerView item.
In the second RecyclerView activity, there is a Spinner. Depending on the item selected in the spinner, different RecyclerViews become visible/invisible to the user on the second activity.
How do I make it work so that the information is sent from the first activity to command the spinner on what to do?This is how Second Activity looks
SecondActivity.java
public class SecondActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TextView tv_title, tv_description;
private ImageView PartiesThumbnailImg,PartiesCoverImg;
private RecyclerView RvPartyMembers;
private PartyMembersAdapter partyMembersAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parties_detail);
Spinner spinner = findViewById(R.id.spnConstituencies);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.constituencies, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
//ini views
iniViews();
//Setting up members list
setupRvPartyMembers();
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
void iniViews() {
RvPartyMembers = findViewById(R.id.rv_party_members);
String partiesTitle = getIntent().getExtras().getString("title");
String partiesDescription = getIntent().getExtras().getString("description");
int imageResourceId = getIntent().getExtras().getInt("imgURL");
int imagecover = getIntent().getExtras().getInt("imgCover");
PartiesThumbnailImg = findViewById(R.id.detail_members_img);
Glide.with(this).load(imageResourceId).into(PartiesThumbnailImg);
PartiesThumbnailImg.setImageResource(imageResourceId);
PartiesCoverImg = findViewById(R.id.detail_members_cover);
Glide.with(this).load(imagecover).into(PartiesCoverImg);
tv_title = findViewById(R.id.tvPartyTitle);
tv_title.setText(partiesTitle);
tv_description = findViewById(R.id.tvPartyDesc);
tv_description.setText(partiesDescription);
}
void setupRvPartyMembers(){
List<PartyMembers> mdata = new ArrayList<>();
mdata.add(new PartyMembers("name",R.drawable.members_brendangriffin_fg));
mdata.add(new PartyMembers("name",R.drawable.members_brendangriffin_fg));
mdata.add(new PartyMembers("name",R.drawable.members_brendangriffin_fg));
partyMembersAdapter = new PartyMembersAdapter(this,mdata);
RvPartyMembers.setAdapter(partyMembersAdapter);
RvPartyMembers.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
}
These are the important pieces of code of the FirstActivity.java
#Override
public void onPartiesItemClick(PartiesOireachtas partiesOireachtas, ImageView partiesImageView) {
Intent intent = new Intent(getContext(),PartiesDetailActivity.class);
intent.putExtra("title", partiesOireachtas.getTitle());
intent.putExtra("description",partiesOireachtas.getDescription());
intent.putExtra("imgURL", partiesOireachtas.getThumbnail());
intent.putExtra("imgCover",partiesOireachtas.getCoverPhoto());
and
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//RecyclerView Setup
//int data
lstPartiesOireachtas = new ArrayList<>();
lstPartiesOireachtas.add(new PartiesOireachtas("Fianna Fáil", "example1", R.drawable.fianna_fail_logo,R.drawable.fianna_fail_cover));
lstPartiesOireachtas.add(new PartiesOireachtas("Sinn Féin", "example2", R.drawable.sinn_fein_logo,R.drawable.sinn_fein_cover));
lstPartiesOireachtas.add(new PartiesOireachtas("Fine Gael", "example4", R.drawable.fine_gael_logo,R.drawable.fine_gael_cover));
lstPartiesOireachtas.add(new PartiesOireachtas("Green Party", "example9", R.drawable.green_party_logo,R.drawable.green_party_cover));
lstPartiesOireachtas.add(new PartiesOireachtas("Social Democrats", "example3", R.drawable.soc_dems_logo,R.drawable.soc_dems_cover));
lstPartiesOireachtas.add(new PartiesOireachtas("Independent", "example2", R.drawable.independent_party_logo,R.drawable.independent_party_cover));
As you probably noticed an activity doesn't have a default constructor, but uses an onCreate(Bundle savedInstanceState) method. This means you should pass your information using this Bundles, or use something like SharedPreferences / SQLite. Since SharedPreferences / SQLite is a bit overkill for this in my opinion, you can add the object upon creating an intent.
Intent intent = new Intent();
intent.putExtra("name", parcelableObject);
If you want to pass a custom object you have to implement the Parcelable interface, this is pretty straightforward since you can basically auto generate all code for this in Android studio (just hit alt-enter a few times). For more information you can check out the following link.
https://developer.android.com/reference/android/os/Parcelable
In the receiving activity you can do the following:
Bundle bundle = getIntent().getExtras();
Object object = bundle.getObject("name"); //Don't forget to cast!

How to retrieve TextView data from another Java class / activity [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I have two Java classes. Where I want to retrieve TextView data from DrinkDetail.java into Cart.java in order to save it into Firebase database. The TextView is deliveryOption and wanted to save in Cart.java from Requests table.
How can I do it? Below are my Java codes.
DrinkDetail.java
public class DrinkDetail extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_detail);
private void showAlertDialogChooseOptions() {
alertDialog = new AlertDialog.Builder(DrinkDetail.this);
alertDialog.setTitle("Drop by our kiosk or rider deliver?");
LayoutInflater inflater = this.getLayoutInflater();
View takeaway_deliver = inflater.inflate(R.layout.takeaway_delivery, null);
btnTakeAway = (FButton)takeaway_deliver.findViewById(R.id.btnTakeAway);
btnDelivery = (FButton)takeaway_deliver.findViewById(R.id.btnDelivery);
deliveryOption = (TextView)takeaway_deliver.findViewById(R.id.deliveryOptionChosen);
alertDialog.setView(takeaway_deliver);
alertDialog.create();
btnTakeAway.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deliveryOption.setText("Take Away");
alertDialogTakeAway.show();
}
});
}
Cart.java:
public class Cart extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
private void showAlertDialog() {
alertDialog2 = new AlertDialog.Builder(Cart.this);
alertDialog2.setTitle("Cash on Delivery:");
alertDialog2.setMessage("Choose amount of money will be given to the rider upon order arriving: ");
LayoutInflater inflater2 = this.getLayoutInflater();
View cash_on_delivery = inflater2.inflate(R.layout.cash_on_delivery, null);
btnOrderNow = (FButton)cash_on_delivery.findViewById(R.id.btnOrderNow);
btnOrderNow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Submit to Firebase
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
editAddress.getText().toString(),
txtTotalPrice.getText().toString(),
"0", //status
editComment.getText().toString(),
paymentMethod.getText().toString(),
moneySelected.getText().toString(),
//WANTED TO SAVE DELIVERY OPTION "TAKE AWAY" HERE,
cart
);
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Order placed
alertDialogOrderPlaced.show();
alertDialog2.setCancelable(true);
}
});
alertDialog2.setView(cash_on_delivery);
alertDialog2.setIcon(R.drawable.icons8_cash_in_hand_filled_50);
}
Make a public method in the class you want to retrieve the data from. Like getData(), something similar to this:
public Data getData(){
return this.data;
}
If your using Intent to navigate from DrinkDetail to Cart then you can pass that string in Intent itself.
like this
Intent intent=new Intent(DrinkDetail.this, Cart.class);
intent.putExtra("Key","String value you want to pass");
startActivity(intent);
and can retrieve that string in Cart.java place below code in onCreate() method
String value;
if(getIntent()!=null)
value=getIntent().getStringExtra("Key");
Data can be stored using SharedPreferences.
In your DrinkDetail.java save the data.
SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
prefs.edit().putString("deliveryOption", deliveryOption.getText()).apply();
In your Cart.java you can retrieve it back.
SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
String deliveryOption = prefs.getString("deliveryOption", "default");
To send the data from one activity to another activity
DrinkDetail.java is first activity from where you want to send data
to another activity
DrinkDetail.java
TextView aText = (TextView )findViewById(R.id.textview);
String text = aText .getText().toString();
Intent myIntent = new Intent(view.getContext(),Cart.java);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
Cart.java is second activity which receive the Intent data whatever
you pass from DrinkDetail.java
Cart.java
public class Cart extends Activity {
TextView mTextview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Cart);
aTextview = (TextView)findViewById(R.id.textView);
aTextview.setText(getIntent().getStringExtra("mytext"));
}

How can I pass the selected items from spinner and checkbox to another activity?

I need to display the selection that I have chosen from spinner and checkbox from one activity to another activity,however I have completed for editText.I have also set the spinner and values for the spinner in arraylist, i have no idea of how to proceed further for spinner and also I don't know how to use checkbox.I need to pass the spinner and checkbox selected values from MainActivity to secondActivity and I need to display the results as a simple textview in secondActivity as follows:
I need to get my final results something like this:
Name: Da1
Email: xxxxxxx#gmail.com
Number: 0123456789
Degree: BE or ME(whatever I am choosing)
Place: Australia
Please help me friends. Many thanks in advance. Herewith I have enclosed my modified coding. But still I am getting values for both Degree and place as null.But I need my result as like the one I specified above.Please let me know the corrections.
MainActivity.java:
public class MainActivity extends Activity implements
AdapterView . OnItemSelectedListener {
EditText Name,Email,Number;
Button submit;
String [] countryNames ={ "India" ,"Australia" ,"Pakistan" ,"Afghanistan" ,"US","Canada","Israel" };
#Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
Name = (EditText)findViewById(R.id.name);
Email = (EditText)findViewById(R.id.email);
Number = (EditText)findViewById(R.id.number);
submit = (Button)findViewById(R.id.submit);
final Spinner spin = (Spinner) findViewById(R.id.simplespinner);
spin.setOnItemSelectedListener(this);
final CheckBox BEBox = (CheckBox)findViewById(R.id.BEBox);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = Name.getText().toString();
String email = Email.getText().toString();
String number = Number.getText().toString();
String spinText = spin.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("Name",name);
intent.putExtra("Email",email);
intent.putExtra("Number",number);
intent.putExtra("spin",spinText);
String checkboxString = null;
if (BEBox.isChecked()) checkboxString = (String) BEBox.getText();
intent.putExtra("checkbox",checkboxString);
startActivity(intent);
}
});
ArrayAdapter aa = new ArrayAdapter ( this , android . R . layout . simple_spinner_item , countryNames );
aa . setDropDownViewResource ( android . R . layout . simple_spinner_dropdown_item );
spin . setAdapter ( aa );
}
#Override
public void onItemSelected ( AdapterView <?> arg0 , View arg1 , int position , long id ) {
Toast . makeText ( getApplicationContext (), countryNames [ position ], Toast . LENGTH_LONG ). show ();
}
#Override
public void onNothingSelected ( AdapterView <?> arg0 ) {
}
}
SecondActivity.java:
public class SecondActivity extends Activity {
String Name;
String Email;
String Number;
String Degree;
String Place;
TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
result = (TextView)findViewById(R.id.result);
Name = getIntent().getExtras().getString("Name");
Email = getIntent().getExtras().getString("Email");
Number = getIntent().getExtras().getString("Number");
Degree = getIntent().getExtras().getString("Degree");
Place = getIntent().getExtras().getString("Place");
result.setText("Name:"+" "+Name+'\n'+"Email:"+" "+Email+'\n'+"Number:"+" "+Number+'\n'+"Degree:"+" "+Degree+'\n'+"Place:"+" "+Place);
}
}
submit = (Button)findViewById(R.id.submit);
Spinner spin = (Spinner) findViewById(R.id.simplespinner);
spin.setOnItemSelectedListener(this);
CheckBox box = (CheckBox) findViewById(R.id.checkbox);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String spinText = spin.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("spin",spinText);
String checkboxString = null;
if (box.isChecked()) checkboxString = box.getText();
intent.putExtra("checkbox",checkboxString);
startActivity(intent);
}
});
If you have a bunch of checkBoxes then you'll have to come up with a better design of getting the values. Maybe you want a RadioButton instead of a CheckBox?

Trying to get string variables from a seperate class causing app to crash

I have simple page to start up the app that has a spinner,editText, and a button. When the user clicks the button I want the app take the selection of the spinner and text and set them to text views in the new class. I tried using getters but when I try this the app crashes.
First Class:
public class MainActivity extends AppCompatActivity {
String pullerName;
String storeName;
Spinner spinner;
EditText etPuller;
public String getStoreName(){
return storeName;
}
public String getPullerName(){
return pullerName;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.store_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Button create = (Button) findViewById(R.id.btnCreate);
etPuller = (EditText) findViewById(R.id.editText);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pullerName = etPuller.getText().toString();
storeName = spinner.getSelectedItem().toString();
Intent i = new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
}
}
Second Class:
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_main);
MainActivity main = new MainActivity();
TextView user = (TextView) findViewById(R.id.tvUser);
TextView store = (TextView) findViewById(R.id.tvStore);
user.setText(main.getPullerName());
store.setText(main.getStoreName());
When this code runs it crashes as soon as i click the button to move to the next activity.
To pass data to a new Activity you need to use Intent.
This is what you need to do:
MainActivity.class:
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pullerName = etPuller.getText().toString();
storeName = spinner.getSelectedItem().toString();
Intent i = new Intent(MainActivity.this,MainActivity2.class);
i.putExtra("puller-name", pullerName);
i.putExtra("store-name", storeName);
startActivity(i);
}
});
In the MainActivity2.class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_main);
TextView user = (TextView) findViewById(R.id.tvUser);
TextView store = (TextView) findViewById(R.id.tvStore);
Intent intent = getIntent();
if(intent != null){ //I always check this to avoid Exceptions
String pullerName = intent.getStringExtra("puller-name");
String storeName = intent.getStringExtra("store-name");
if(pullerName != null) user.setText(pullerName);
if(storeName != null) store.setText(storeName);
}
}
You was creating a new instance of MainActivity in MainActivity2. So, when you called the getters of this mainActivity will return null, because these values were never setters. If you want to use your method, you need to pass the same instance of the activity to MainActivity2, but this is not recommended. The official method to pass data to another activity is via intent. To learn more about Intent learn the official Android documentation: https://developer.android.com/training/basics/firstapp/starting-activity.html
You have used wrong way to get values from MainActivity to MainActivity2. Simply you can pass values using the technique below.
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("pullerName", pullerName);
To use this value pullerName in MainActivity2 class, just use the technique below.
String value = getIntent().getExtras().getString("pullerName");`

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'
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.
My classes are displayed below..
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.
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.
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;
}
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.
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
Any help would be much appreciated!
Use Bundle like this if you want to pass data while creating the Fragment.
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. In your case even you can access the Intent associated with the FragmentActivity and access the extra values from it.
public void putFloat (String key, float value)

Categories