public class MoreQP extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
FirebaseDatabase database;
DatabaseReference mDatabase;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_qp);
filterSearchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showResult();
}
});
resultCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent showresulPage = new Intent(MoreQP.this, ShowQuestion.class);
showresulPage.putExtra(mDatabase);
showresulPage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
MoreQP.this.startActivity(showresulPage);
finish();
}
});
}
public void showResult(){
resultDisplay.setText(selectedSubject+"\n"+sy+" "+ss+" "+se);
resultCard.setVisibility(resultCard.VISIBLE);
//"selectedSubject","sy","ss" etc.. I have taken these values from multiple spinner.
mDatabase = FirebaseDatabase.getInstance().getReference().child("Department").child("CSE").child("Year").child(sy).child(ss).child(se).child(whichSemester).child(selectedSubject);
}
}
Here is the short-cut version of my code. I want to pass Firebase reference "mDatabase" to my 2nd Activity. I took all those values from multiple spinners. So is there any way to pass this reference to new activity? And how I can receive it on 2nd activity?
There is no need to add the DatabaseReference object to an Intent in order to use it to another activity, just simply create the same DatabaseReference and use it in the second activity. That's it!
You can not put Storage / Database Reference in putExtra but you can certainly put string value of path
'String storageRef = "group/"+UID+"/"+"profileImage";
String postUploadRef="User/"+UID;'
Intent mServiceIntent = new Intent(ProfileActivity.this, UploadService.class);
mServiceIntent.putExtra("stream", filePath);
mServiceIntent.putExtra("stroageRef", storageRef);
mServiceIntent.putExtra("postUploadRef", postUploadRef);
startService(mServiceIntent);
Related
I've got little problem with implementing asynctask for my class project.
"PART_OF_URL?title="+tytul+"&author="+autor+"&date="+dataSt+"&username="+imieSt
so this is whole string that should be executed, it works "perfectly" fine like this:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("PART_OF_URL?title="+tytul+"&author="+autor+"&date="+dataSt+"&username="+imieSt));
startActivity(i);
Which sends data to PART_OF_URL website that display order list
but instead of that it should be done in asynctask.
Here is whole activity code that handle sending data:
public class Zamowienie extends AppCompatActivity {
TextView tytulzamowieniaTV;
EditText data;
EditText imie;
Button zamawiaj;
Button anuluj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zamowienie);
tytulzamowieniaTV=findViewById(R.id.tytulZamwowienia);
data=findViewById(R.id.data);
imie=findViewById(R.id.imie);
zamawiaj=findViewById(R.id.zamawiaj);
anuluj=findViewById(R.id.anuluj);
tytulzamowieniaTV.setText(getIntent().getStringExtra("Tytul"));
String tytul=getIntent().getStringExtra("Tytul");
String autor=getIntent().getStringExtra("Autor");
wyslijZamowienie(tytul,autor);
}
public void wyslijZamowienie(String tytul,String autor){
zamawiaj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String dataSt=data.getText().toString();
String imieSt=imie.getText().toString();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("PART_OF_URL?title="+tytul+"&author="+autor+"&date="+dataSt+"&username="+imieSt));
startActivity(i);
}
});
}
}
Understanding asynctask is problematic for me so any help is appreciated.
I want to define callback for a Notebook program that way after note saved in EditActivity,in Main Activity Update list of notes,But This does not happen.
EditActivity:
public interface OnClickDoneListener{
void onClickDone();
}
public void setOnClickDoneListener(OnClickDoneListener onClickDoneListener){
this.onClickDoneListener=onClickDoneListener;
}
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (extras != null) {
myDatabase.updateRow(id, txtTitle.getText().toString(), txtDesc.getText().toString());
NoteModel noteModel = new NoteModel();
noteModel.setTitle(txtTitle.getText().toString());
noteModel.setDesc(txtDesc.getText().toString());
Intent intent = new Intent(EditActivity.this, MainActivity.class);
startActivity(intent);
onClickDoneListener.onClickDone();
} else {
Done();
}
}
});
Main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setupViews();
getDataFromDB();
recyclerView.setAdapter(new NoteAdapter(MainActivity.this,dataList));
noteAdapter = new NoteAdapter(MainActivity.this,dataList);
recyclerView.setAdapter(noteAdapter);
fabAdd = (FloatingActionButton)findViewById(R.id.fab_main_add) ;
EditActivity editActivity = new EditActivity();
editActivity.setOnClickDoneListener(new EditActivity.OnClickDoneListener() {
#Override
public void onClickDone() {
noteAdapter.notifyDataSetChanged();
}
});
please help me.
My English is poor,sorry for it.
You want your data to be updated when the MainActivity is shown, right ?
You just have to call your getDataFromDB part in onResume() instead of onCreate()
EditActivity editActivity = new EditActivity();
editActivity.setOnClickDoneListener(new EditActivity.OnClickDoneListener() {
#Override
public void onClickDone() {
noteAdapter.notifyDataSetChanged();
}
});
What you are doing here is setting the value of OnClickDoneListener in a new instance of EditActivity that you will never use. Because when you use new Intent(MainActivity.this, EditActivity.class) to start the EditActivity it will create a new instance of EditActivity and your interface would be null.
I suggest you use android's Broadcast Receiver instead of a callback in this case. All you have to do is:
In MainActivity: You will need to instantiate a broadcast receiver. You need to register it onCreate or onStart and unregister it onStop or onDestory.
In EditAcitivity: You send a broadcast whenever you want to notify your MainActivity to update the list.
Check out this example:
https://riptutorial.com/android/example/18305/communicate-two-activities-through-custom-broadcast-receiver
I just figured out how to use intent to pass variables to other activity ,but I found out that it has a limit , it can only pass one variable to one activity .
I want to know how to share the variable in multiple activity like I have this variable in activity 1 and I want to use it in activity 2 and activity 3.
this is my activity 1 , the variable I want to use in other activity is this
(double ans=(90+4.8*nh+13.4*nw-5.7*na);)
I figured out how to pass it to activity 2 but I can't pass it to activity 3. Is there an easy way to pass the variable in multiple activity , because I'm kinda new to Android and OOP there's a lot of stuff I don't really get just from searching. Thank you so much!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_male);
age =(TextView)findViewById(R.id.age);
height=(TextView)findViewById(R.id.heigth);
weight =(TextView)findViewById(R.id.weigth);
result =(TextView)findViewById(R.id.result);
eage=(EditText)findViewById(R.id.eage);
eheight=(EditText)findViewById(R.id.eheight);
eweight=(EditText)findViewById(R.id.eweight);
calculate=(Button)findViewById(R.id.calculate);
back =(Button)findViewById(R.id.back);
next =(Button)findViewById(R.id.next);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double na=Double.parseDouble(eage.getText().toString());
double nh=Double.parseDouble(eheight.getText().toString());
double nw=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*nh+13.4*nw-5.7*na);
result.setText("Your BMR is "+ans);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(MALE.this,MainActivity.class);
startActivity(i);
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MALE.this, MALERUN.class);
double na=Double.parseDouble(eage.getText().toString());
double nh=Double.parseDouble(eheight.getText().toString());
double nw=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*nh+13.4*nw-5.7*na);
result.setText("Your BMR is "+ans);
i.putExtra("answer",ans);
i.putExtra("age",na);
i.putExtra("height",nh);
i.putExtra("weight",nw);
startActivity(i);
}
});
}
Use SingleTon Pattern.
public class SingletonClass {
private double yourValue;
private static SingletonClass sSoleInstance;
private SingletonClass() {
} // private constructor.
public static SingletonClass getInstance() {
if (sSoleInstance == null) {
sSoleInstance = new SingletonClass();
}
return sSoleInstance;
}
public void setValue(double val) {
yourValue = val;
}
public double getValue() {
return yourValue;
}
}
Set Variable from Activity1
SingletonClass.getInstance().setValue(yourValue);
Now you can access this value across application.
SingletonClass.getInstance().getValue();
For more details click here
Using SharedPreferences you can get it.here is examples
next.setOnClickListener{
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("answer",ans);
editor.putString("age",""+na);
editor.putString("height",""+nh);
editor.putString("weight",""+nw);
editor.commit();
Intent i=new Intent(MALE.this,MainActivity.class);
startActivity(i);
}
Any Another Activity you can get it below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
String answer = sharedPref.getString("answer",null);
String age = sharedPref.getString("age",null);
Log.e("resultsss",""+answer + " age : "+age);
}
Solution 2:
Either you have to use Sqlite database
suggestion: if you have very less fields better to use
SharedPreferences if you have little hug data better go with Sqlite
database
You can declare a varible :
public class MainActivity extends AppCompatActivity {
// Like below
public static double ans;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
}
And call it from everywhere like this:
MainActivity.ans;
You can declare a different variable anytime you want and it will change in every Activity.
just do this :
MainActivity.ans = 123.123;
I need help.
I create android recyclerview cardview project, when data list from cardview clicked (use putExtra dan getExtra) will call new activity , but not working (in new activity blank no content).
this my code (CardViewAndroidAdapter.java) my full code error when i paste in here - ask in stackoverflow
holder.btnDetail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("PUT_PHOTO", getListAndroid().get(position).getPhoto());
intent.putExtra("PUT_DESK", getListAndroid().get(position).getDesk());
context.startActivity(intent);
}
});
this my code (DetailActivity.java) my full code error when i paste in here
public class DetailActivity extends AppCompatActivity {
private static final String GET_PHOTO = "get_photo";
private static final String GET_DESK = "get_desk";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setTitle("Detail Version");
getIncomingIntent();
}
private void getIncomingIntent() {
if(getIntent().hasExtra(GET_PHOTO)&&getIntent().hasExtra(GET_DESK)){
String getPhoto=getIntent().getStringExtra(GET_PHOTO);
String getDesk = getIntent().getStringExtra(GET_DESK);
TextView detail=findViewById(R.id.tv_detail);
detail.setText(getDesk);
ImageView img_item_photo=findViewById(R.id.img_item_photo);
Glide.with(this)
.load(getPhoto)
.into(img_item_photo);
}
}
Replace this:
intent.putExtra("PUT_PHOTO", getListAndroid().get(position).getPhoto());
intent.putExtra("PUT_DESK", getListAndroid().get(position).getDesk());
with this:
intent.putExtra(DetailActivity.GET_PHOTO, getListAndroid().get(position).getPhoto());
intent.putExtra(DetailActivity.GET_DESK, getListAndroid().get(position).getDesk());
It should work now.
I will make anonym function onClick for new widget. I load variable data from res/raw. But i need some ID for load it. I use getResources().getIdentifier()
It looks like this:
TextView ans = new TextView(this);
String exm = "demo";
ans.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FullscreenActivity.this, Video.class);
intent.putExtra("ex", getResources().getIdentifier( exm,"raw", getPackageName()));
startActivity(intent);
}
}
);
I have error about variable non inner class variable. But i can't final it - it will be variable. How can i send my variable in new Object OnClickListener and function onClick ?
have it as a class variable
public class MyActivity extends Activity {
String exm = "demo";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ans.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FullscreenActivity.this, Video.class);
intent.putExtra("ex", getResources().getIdentifier( exm,"raw", getPackageName()));
startActivity(intent);
}
}
);
}
}
The easiest way is to make it a member variable of your containing Activity.