How to display both data in Edittext without losing one of them? - java

I have two Edittexts in MainActivity: etFrom and etTo.
When clicked etFrom goes to ListFrom activity and etTo to ListTo activity.ListFrom and ListTo activities contain Listview and after choosing an item it comes back to Main activity and displays results in edittexts. I use MainActivity.putExtra("To",v); and etTo.setText(getIntent().getStringExtra("To")); to get data from an activity.
After clicking on first edittext, choosing an item from Listview and coming back to Main Activity, when I do the same for second edittext the first edittext becomes empty. And the same when I do first one, second one becomes empty.First Second
ListTo Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_to);
etSearchTo=(EditText) findViewById(R.id.etSearchTo);
listViewTo=(ListView) findViewById(R.id.listViewTo);
initList();
listViewTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
Intent intent=new Intent(ListTo.this,SearchRide.class);
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
intent.putExtra("To",v);
startActivity(intent);
}
});
ListFrom Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_from);
listView=(ListView) findViewById(R.id.listviewFrom);
editText=(EditText) findViewById(R.id.etsearchFrom);
initList();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
Intent intent=new Intent(ListFrom.this,SearchRide.class);
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
intent.putExtra("From",v);
startActivity(intent);
}
});
SearchRide as Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_ride);
etDate=(EditText) findViewById(R.id.etDate);
TextView tFrom=(TextView) findViewById(R.id.tFrom);
TextView tTo=(TextView) findViewById(R.id.tTo);
etFrom=(EditText) findViewById(R.id.etFrom);
etTo=(EditText) findViewById(R.id.etTo);
Button bSearch=(Button) findViewById(R.id.bSearch);
ImageView iplussign=(ImageView) findViewById(R.id.iplussign);
ImageView iminussign=(ImageView) findViewById(R.id.iminussign);
final TextView tNumber=(TextView) findViewById(R.id.tNumber);
etTo.setText(getIntent().getStringExtra("To"));
tNumber.setText(String.valueOf(p));
iplussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p<=9) {
p++;
tNumber.setText(String.valueOf(p));
}
}
});
iminussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p>=2) {
p--;
tNumber.setText(String.valueOf(p));
}
}
});
etFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ListFrom=new Intent(SearchRide.this,ListFrom.class);
startActivity(ListFrom);
}
});
etFrom.setText(getIntent().getStringExtra("From"));
etTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ToList =new Intent(SearchRide.this,ListTo.class);
startActivity(ToList);
}
});
etTo.setText(getIntent().getStringExtra("To"));
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerDialog dialog= new DatePickerDialog(SearchRide.this,R.style.MyDatePickerStyle,listener,calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.MONTH),calendar.get(Calendar.YEAR));
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dialog.show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

use startActivityForResult() to start ListFrom and ListTo Activities and the receive the result from these activities in OnActivityResult() method.
SearchRide as Main Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_ride);
etDate=(EditText) findViewById(R.id.etDate);
TextView tFrom=(TextView) findViewById(R.id.tFrom);
TextView tTo=(TextView) findViewById(R.id.tTo);
etFrom=(EditText) findViewById(R.id.etFrom);
etTo=(EditText) findViewById(R.id.etTo);
Button bSearch=(Button) findViewById(R.id.bSearch);
ImageView iplussign=(ImageView) findViewById(R.id.iplussign);
ImageView iminussign=(ImageView) findViewById(R.id.iminussign);
final TextView tNumber=(TextView) findViewById(R.id.tNumber);
tNumber.setText(String.valueOf(p));
iplussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p<=9) {
p++;
tNumber.setText(String.valueOf(p));
}
}
});
iminussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p>=2) {
p--;
tNumber.setText(String.valueOf(p));
}
}
});
etFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ListFrom = new Intent(SearchRide.this,ListFrom.class);
startActivityForResult(ListFrom,0);
}
});
etTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ToList = new Intent(SearchRide.this,ListTo.class);
startActivityForResult(ToList,1);
}
});
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerDialog dialog= new DatePickerDialog(SearchRide.this,R.style.MyDatePickerStyle,listener,calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.MONTH),calendar.get(Calendar.YEAR));
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dialog.show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if(resultCode == Activity.RESULT_OK){
etFrom.setText(data.getStringExtra("From"));
}
} else if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
etTo.setText(data.getStringExtra("To"));
}
}}
ListFrom code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_from);
listView=(ListView) findViewById(R.id.listviewFrom);
editText=(EditText) findViewById(R.id.etsearchFrom);
initList();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
Intent intent = new Intent();
intent.putExtra("From",v);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
ListTo code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_to);
etSearchTo=(EditText) findViewById(R.id.etSearchTo);
listViewTo=(ListView) findViewById(R.id.listViewTo);
initList();
listViewTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
Intent intent = new Intent();
intent.putExtra("To",v);
setResult(Activity.RESULT_OK, intent);
finish();
}
});

Here is what you can do. Put both from and to values as extra when starting an activity and return them both as result. This way you will be able to remember it. I am assuming you just do not want to use static variables.
// in ListTo activity
intent.putExtra("To",v);
intent.putExtra("From", getIntent().getStringExtra("From"));
// in ListFrom activity
intent.putExtra("From",v);
intent.putExtra("To", getIntent().getStringExtra("To"));
// in Main activity
etTo.setText(getIntent().getStringExtra("To"));
etFrom.setText(getIntent().getStringExtra("From"));
// when starting ListFrom activity
Intent ListFrom=new Intent(SearchRide.this,ListFrom.class);
intent.putExtra("From", getIntent().getStringExtra("From"));
intent.putExtra("To", getIntent().getStringExtra("To"));
startActivity(ListFrom);
// when starting ListTo activity
Intent ToList =new Intent(SearchRide.this,ListTo.class);
intent.putExtra("From", getIntent().getStringExtra("From"));
intent.putExtra("To", getIntent().getStringExtra("To"));
startActivity(ToList);
PS: I have not tested the above code. You can experience null pointer errors, but it should give you the general idea.

Related

Cannot get the secondSpinner to refresh itself

Can someone look at my code its really simple created just to practice my skills. But the language spinner works but the gender spinner is buggy. It will work once i change the language then change the gender and then go back to the gender i originally wanted. Code is shown. Thanks. Have tried putting onItemSelected() for spinnerGen, it does not solve the issue. Researching about this since last night cant find any solution
public class MainActivity extends AppCompatActivity {
private Button btnGo, btnAbout;
private Spinner spinner, spinnerGen;
private EditText addName;
private ArrayList<String> options;
private ArrayList<String> optionsGen;
private String allOption = "";
private String genOption = "";
private String genOptionSec = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPage();
options = new ArrayList<>();
options.add("English");
options.add("Bengali");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, options);
spinner.setAdapter(adapter);
adapter.setNotifyOnChange(true);
adapter.notifyDataSetChanged();
optionsGen = new ArrayList<>();
optionsGen.add("Male");
optionsGen.add("Female");
ArrayAdapter<String> adapterGen = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, optionsGen);
spinnerGen.setAdapter(adapterGen);
adapterGen.setNotifyOnChange(true);
adapterGen.notifyDataSetChanged();
spinnerMethod();
}
private void initPage() {
btnGo = findViewById(R.id.btnGo);
btnAbout = findViewById(R.id.btnAbout);
spinner = findViewById(R.id.spinner);
spinnerGen = findViewById(R.id.spinnerGen);
addName = findViewById(R.id.addName);
}
public void spinnerMethod() {
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
allOption = spinner.getSelectedItem().toString();
switch (allOption) {
case "English" :
newMethodEnglish();
break;
case "Bengali" :
newMethodBengali();
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void newMethodEnglish() {
genOption = spinnerGen.getSelectedItem().toString();
switch (genOption) {
case "Male":
onClickEngMale(); //In these method Im just navigating to another activity
break;
case "Female":
OnClickEngFemale();
break;
default:
break;
}
}
public void OnClickEngFemale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String femaleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, EngFemale.class);
intent.putExtra("DataFemale", femaleData);
startActivity(intent);
}
});
}
public void onClickEngMale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String maleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, EngMale.class);
intent.putExtra("Data", maleData);
startActivity(intent);
}
});
}
public void newMethodBengali() {
genOptionSec = spinnerGen.getSelectedItem().toString();
switch (genOptionSec){
case "Male":
onClickBanMale();
break;
case "Female":
onClickBanFemale();
default:
break;
}
}
public void onClickBanMale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String maleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, BanMale.class);
intent.putExtra("DataBan", maleData);
startActivity(intent);
}
});
}
public void onClickBanFemale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String femaleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, BanFemale.class);
intent.putExtra("BanFemale", femaleData);
startActivity(intent);
}
});
}
}

How to properly display a custom dialog in an adapter

I have a custom adapter in my app, and i launch several activities from there, one of the populated lists in the adapter takes you back to the login screen which works fine, but I decided to add a confirmation dialog before taking the user to the login screen, but I get an error saying android.content.ActivityNotFoundException: No Activity found to handle Intent { }
This is what I've implemented so far
AccountAdapter.java
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title;
ImageView icon;
MyViewHolder(View view) {
super(view);
view.setClickable(true);
view.setOnClickListener(this);
mCtx = view.getContext();
title = view.findViewById(R.id.title);
icon = view.findViewById(R.id.icon);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
switch (getAdapterPosition()){
case 0:
intent = new Intent(mCtx, SettingsActivity.class);
break;
case 1:
intent = new Intent(mCtx, ProfileActivity.class);
break;
case 2:
intent = new Intent(mCtx, ChangePasswordActivity.class);
break;
default:
CustomAlertDialog alert = new CustomAlertDialog((Activity)mCtx);//faulty code
alert.show();
break;
}
mCtx.startActivity(intent);
}
}
CustomAlertDialog.java
public class CustomAlertDialog extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
// public Button yes, no;
public TextView yes, no;
public CustomAlertDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_alert_dialog);
yes = (TextView) findViewById(R.id.btn_yes);
// no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
// no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
break;
default:
break;
}
dismiss();
}
}
AccountFragment.java (where I populate the account adapter)
private void prepareAccountData() {
// if (mAdapter == null) {
// return;
// }
AccountModel options = new AccountModel("Settings", R.drawable.ic_action_settings);
accountList.add(options);
options = new AccountModel("Update Profile", R.drawable.ic_social_person_outline);
accountList.add(options);
options = new AccountModel("Change Password", R.drawable.ic_hardware_security);
accountList.add(options);
options = new AccountModel("Logout", R.drawable.ic_action_exit_to_app);
accountList.add(options);
mAdapter.notifyDataSetChanged();
}
and this is the code I use for direct logout which works aswell
((Activity)mCtx).finish();
SharedPreferences sharedPreferences = mCtx.getSharedPreferences("key", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
intent = new Intent(mCtx, LoginActivity.class);
How can I implement the custom dialog in my adapter?
Your code has bug you don't init intent in case show dialog, you should move logic into onClick in your Dialog, move this mCtx.startActivity(intent); into case
#Override
public void onClick(View v) {
switch (getAdapterPosition()){
case 0:
mCtx.startActivity(new Intent(mCtx, SettingsActivity.class));
break;
case 1:
mCtx.startActivity(new Intent(mCtx, ProfileActivity.class));
break;
case 2:
mCtx.startActivity(new Intent(mCtx, ChangePasswordActivity.class));
break;
default:
CustomAlertDialog alert = new CustomAlertDialog((Activity)mCtx)
alert.show();
break;
}
}

How to update image in fragment when selecting an image in an Activity?

Hello I'm trying to select an Image in an activity page and that selected image to update in my Fragment page. I've tried what I've learnt previously, but it's not working at all. Below is what I've attempted. I'm still new to android development, so please bear with me.
ProfileSettingsActivity.java:
public class ProfileSettingsActivity extends AppCompatActivity {
ImageView profileImage1;
ImageView profileImage2;
ImageView profileImage3;
ImageView profileImage4;
ImageView profileImage5;
ImageView profileImage6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_settings);
profileImage1 = (ImageView) findViewById(R.id.teamid00);
profileImage2 = (ImageView) findViewById(R.id.teamid01);
profileImage3 = (ImageView) findViewById(R.id.teamid02);
profileImage4 = (ImageView) findViewById(R.id.teamid03);
profileImage5 = (ImageView) findViewById(R.id.teamid04);
profileImage6 = (ImageView) findViewById(R.id.teamid05);
profileImage1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
}
public void setProfilePicture(View view) {
//Creating a Return intent to pass to the Main Activity
Intent returnIntent = new Intent();
//Figuring out which image was clicked
ImageView selectedImage = (ImageView) view;
//Adding stuff to the return intent
returnIntent.putExtra("imageID", selectedImage.getId());
setResult(RESULT_OK, returnIntent);
//Finishing Activity and return to main screen!
finish();
}
}
Below is the Fragment that contains the page I start on from and go to the ProfileSettingsActivity page
PeopleFragment.java:
public class PeopleFragment extends Fragment {
ImageButton settingsButton;
PieChart pieChart;
ImageButton profileImage;
private FirebaseAuth mAuth;
TextView fullName;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_people2, container,
false);
profileImage = (ImageButton) view.findViewById(R.id.imageButton4);
profileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent settingsClick = new Intent(getActivity(),
ProfileSettingsActivity.class);
getActivity().startActivityForResult(settingsClick,0);
}
});
fullName = (TextView) view.findViewById(R.id.userProfileFullName);
mAuth = FirebaseAuth.getInstance();
return view;
}
#Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
/**
* Updates the view according to the authentication status.
* #param user the current FirebaseUser
*/
private void updateUI(FirebaseUser user) {
if (user != null) {
fullName.setText(user.getDisplayName());
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_CANCELED) return;
//Getting the Avatar Image we show to our users
ImageView avatarImage =
(ImageView)getView().findViewById(R.id.imageButton4);
//Figuring out the correct image
String drawableName = "profile1";
switch (data.getIntExtra("imageID",R.id.teamid00)) {
case R.id.teamid00:
drawableName = "profile1";
break;
case R.id.teamid01:
drawableName = "profile2";
break;
case R.id.teamid02:
drawableName = "profile3";
break;
case R.id.teamid03:
drawableName = "profile4";
break;
case R.id.teamid04:
drawableName = "profile5";
break;
case R.id.teamid05:
drawableName = "profile6";
break;
default:
drawableName = "profile1";
break;
}
int resID = getResources().getIdentifier(drawableName, "drawable",
getActivity().getPackageName());
avatarImage.setImageResource(resID);
}
}
Currently with what I have above, I can move from the fragment page to the activity page and I can press on the buttons to select the images and then the activity closes for me. But when I'm back on the fragment page, the profile image isn't updated at all, and displays the default one.
Try to use startActivityForResult() instead of getActivity().startActivityForResult(). So, change your Fragment code to:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
profileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent settingsClick = new Intent(getActivity(),
ProfileSettingsActivity.class);
startActivityForResult(settingsClick,0);
}
});
...
return view;
}
Note: This is not related to your problem.
You can simplify you activity code to this:
public class ProfileSettingsActivity extends AppCompatActivity {
ImageView profileImage1;
ImageView profileImage2;
ImageView profileImage3;
ImageView profileImage4;
ImageView profileImage5;
ImageView profileImage6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_settings);
profileImage1 = (ImageView) findViewById(R.id.teamid00);
profileImage2 = (ImageView) findViewById(R.id.teamid01);
profileImage3 = (ImageView) findViewById(R.id.teamid02);
profileImage4 = (ImageView) findViewById(R.id.teamid03);
profileImage5 = (ImageView) findViewById(R.id.teamid04);
profileImage6 = (ImageView) findViewById(R.id.teamid05);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
};
profileImage1.setOnClickListener(clickListener);
profileImage2.setOnClickListener(clickListener);
profileImage3.setOnClickListener(clickListener);
profileImage4.setOnClickListener(clickListener);
profileImage5.setOnClickListener(clickListener);
profileImage6.setOnClickListener(clickListener);
}
public void setProfilePicture(View view) {
//Creating a Return intent to pass to the Main Activity
Intent returnIntent = new Intent();
//Figuring out which image was clicked
ImageView selectedImage = (ImageView) view;
//Adding stuff to the return intent
returnIntent.putExtra("imageID", selectedImage.getId());
setResult(RESULT_OK, returnIntent);
//Finishing Activity and return to main screen!
finish();
}
}
Probably you've got some problems with your onActivityResult method. Check these answers.
Also there is a solution that is going to work:
1. Tag your fragments when you create them:
FragmentTransaction fTrans = getFragmentManager().beginTransaction();
fTrans.add(R.id.main_layout, new PeopleFragment(), "PeopleFragmentTag"));
fTrans.commit();
2. Write method in PeopleFragment, which could setImage:
public void updateProfileImage(int profileImgID){
/* Update your image here */
}
3. Get a fragment by tag and call the method:
public void setProfilePicture(View view) {
/* Your code */
PeopleFragment peopleFragment = (PeopleFragment) getFragmentManager().findFragmentByTag("PeopleFragmentTag");
peopleFragment.updateProfileImage(selectedImage.getId());
}
Hope, I helped you somehow. Good luck!

Passing Data activity to TabView in android

I want to pass data from main activity to one tab of TabView.Here is my code and it isn't working,what to do now
There is my codes
MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.getInfoButton);
text= (EditText) findViewById(R.id.gitHubUserName);
Logger.addLogAdapter(new AndroidLogAdapter());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
Logger.d(user);
Bundle bundle=new Bundle();
bundle.putString("NAME",user);
ProfileActivity pro = new ProfileActivity();
pro.setArguments(bundle);
startActivity(in);
}
});
}
}
my Adapter for tabViews :
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
and this is ProfileActivity where I want to pass the data which I got from EditText in Main Activity :
public class ProfileActivity extends Fragment {
ImageView imageView;
TextView name;
TextView bio;
String text;
public void dataPass(String txt){
text = txt;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_profile, container, false);
name = rootView.findViewById(R.id.name);
bio = rootView.findViewById(R.id.bio);
Logger.addLogAdapter(new AndroidLogAdapter());
Bundle bundle = this.getArguments();
String nameString = bundle.getString("NAME");
Logger.d(nameString);
name.setText(nameString);
return rootView;
}
}
when I run the app, app stopped working and in logcat this error :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.salman.getgithubofgeek.Activity.Activity.Activites.ProfileActivity.onCreateView(ProfileActivity.java:43)
You cannot use setArguments like that.
Do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
in.putExtra("NAME",user);
Logger.d(user);
startActivity(in);
}
});
then in Tab activity, pass the extra to the adapter wherever you call it like so:
PagerAdapter adapter = new PagerAdapter(fm, user);
and make the following changes to the adapter class:
public class PagerAdapter extends FragmentPagerAdapter {
String nameString;
public PagerAdapter(FragmentManager fm, String nameString) {
super(fm);
this.nameString = nameString;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
Bundle bundle=new Bundle();
bundle.putString("NAME",nameString);
profileActivity.setArguments(bundle);
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
The problem is you are trying to add the bundle in fragment that is in `
TabActivity
and inPager` you are again declaring ProfileActivity fragment that is with out
setArguments()
.
All you need to do is send the bundle to activity first and then before using ProfileActivity fragment in Pager set the bundle to the ProfileActivity fragment.
Button button;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.getInfoButton);
text= (EditText) findViewById(R.id.gitHubUserName);
Logger.addLogAdapter(new AndroidLogAdapter());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
Logger.d(user);
//modification goes here
intent.putString("NAME",user);
startActivity(in);
}
});
}
}
and
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
//modifications goes here
Bundle b = new Bundle();
bundle.putString("NAME",user);
profileActivity.setArguments(b);
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}

Android putExtra push data with same key from different buttons

I have a code that uses putextra method to push data from activity to another, I want to push different values using the same key
code:
String int_value = "int_value";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondScreen.class);
intent.putExtra(int_value , 0);
startActivity(intent);
}
});
Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondScreen.class);
intent.putExtra(int_value , 1);
startActivity(intent);
}
});
And in the next activity:
int value;
String int_value = "int_value";
View myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_screen);
Intent intent = getIntent();
int temp = intent.getIntExtra(int_value, value);
myLayout = findViewById(R.id.myLayout);
switch (value){
case 0:
myLayout.setBackgroundResource(R.drawable.a);
Log.e("VALUE" , String.valueOf(value));
break;
case 1:
myLayout.setBackgroundResource(R.drawable.b);
Log.e("VALUE" , String.valueOf(value));
break;
}
But the background always changes to a.jpg although I passed 0 & 1 (or a least I thought I did...)
what is the problem here?
just change value in switch to temp
switch ( temp){
case 0:
myLayout.setBackgroundResource(R.drawable.a);
Log.e("VALUE" , String.valueOf(value));
break;
case 1:
myLayout.setBackgroundResource(R.drawable.b);
Log.e("VALUE" , String.valueOf(value));
break;
}
because you are saving the value inside temp
int temp = intent.getIntExtra(int_value, value);

Categories