This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I am doing admin page. I want to make changes to the data from server. The data already retrieved from server, but I do not know how to data can carry to the next activity(when i click Edit) using putExtra, because I use another java class to for retrieving the information. This is the sample of my table.
Below is my java coding:
public class assessment_table_edit extends AppCompatActivity {
Toolbar toolbar;
String data = "";
TableLayout tlAssessment;
TableRow tr;
TextView stuID,totalmarks,marks,edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assessment_table_edit);
tlAssessment=(TableLayout)findViewById(R.id.tlAssessment_Edit);
final Assessment_Information_GetData getdb=new Assessment_Information_GetData();
new Thread(new Runnable() {
#Override
public void run() {
data =getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<Assessment_Information> users=parseJSON(data);
addData(users);
}
});
}
}).start();
}
View.OnClickListener onClickListener=new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.assessment_id:
Intent iChange=new Intent(assessment_table_edit.this,change_details.class);
//Having problem here
iChange.putExtra();
startActivity(iChange);
break;
}
}
};
Appreciate if some can enlighten me on how to use putExtra or other method to carry the data to another activity.
You can send data using,
Intent iChange = new Intent(assessment_table_edit.this,change_details.class);
iChange.putExtra("YOUR_KEY", "YOUR_VALUE");
startActivity(iChange);
You can get data using,
String data = getIntent().getStringExtra("YOUR_KEY");
You can pass your Object through Intent by using putExtra() method
if you want to pass arraylist then you can use putParcelableArrayListExtra()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this,SecondActivity.class);
ArrayList<Assessment_Information> testing = new ArrayList<Assessment_Information>();
i.putParcelableArrayListExtra("extraextra", testing);
startActivity(i);
}
SecondActivity
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Assessment_Information> testing = this.getIntent().getParcelableArrayListExtra("extraextra");
}
}
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 read some example on this website and other but still have error.
It's not a compilation error but my application crash when I click on the button.
There is code of my MainActivity.java (only interesting part) :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//blablabla
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myLayout= (LinearLayout) findViewById(R.id.layoutProp);
Button button = (Button) findViewById(R.id.buttonValider);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT, text); // this one does not work
intent.putExtra(EXTRA_NUMBER, nbTextView);
startActivity(intent);
}
}
And code in Activity2.java
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
int nbTextView = intent.getIntExtra(MainActivity.EXTRA_NUMBER, 0);
MyTextView[] text = (MyTextView[]) intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
TextView textView1 = (TextView) findViewById(R.id.TVtest);
textView1.setText(""+ nbTextView);
} }
if I comment : MyTextView[] text =(MyTextView[])intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
the application does not crash.
Thank you all very much for your help
You cannot pass views through intents. Views are part of the layout and cannot be shared between activities.
If you want to pass array with custom objects you must implement a Parcelable or Serializable class and use:
getIntent().getParcelableExtra("array");
or
getIntent().getSerializableExtra("array");
Parcelable class will be faster but it is harder to implement.
Another way is to use a library like Gson to parse your data to Json and pass it as a string.
ANSWERED
I am trying to send and int to another activity.There is a button in first act. and it opens second act. and takes int from here to other page.But i cant start two of them at one time.
Here is my first activity :
public class ana_ekran extends AppCompatActivity {
public TextView ana_ekran_kule;
public TextView ana_ekran_can;
public int ana_ekran_can_int=30;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ana_ekran);
ana_ekran_kule=(TextView) findViewById(R.id.textView);
ana_ekran_can=(TextView) findViewById(R.id.textView2);
ana_ekran_can.setText(ana_ekran_can_int+" CAN");
}
public void devam (View v){
Intent i = new Intent(getApplicationContext(),fight_1.class);
i.putExtra("deger", ana_ekran_can_int);
startActivity(i);
startActivity(new Intent(this,fight_1.class));
}
}
and this is second:
public class fight_1 extends AppCompatActivity {
public TextView fight_1_can;
public int fight_1_can_int;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fight_1);
fight_1_can=(TextView) findViewById(R.id.textView3);
int i = getIntent().getIntExtra("deger",-1);
fight_1_can_int=i;
fight_1_can.setText(fight_1_can_int+"");
}
}
This startActivity(i); will do the same thing as next line but it will carry the data as well whereas startActivity(new Intent(this,fight_1.class)); will only start the other instance of fight_1 activity so
public void devam (View v){
Intent i = new Intent(getApplicationContext(),fight_1.class);
i.putExtra("deger", ana_ekran_can_int);
startActivity(i);
// start fight_1 again without any data so not required
//startActivity(new Intent(this,fight_1.class));
}
I have a grand parent activity called Department
public class Department extends AppCompatActivity {
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_department);
.........
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DeptDetail.class);
Bundle extra = new Bundle();
extra.putString("Department", getAdapterPosition()+"");
intent.putExtras(extra);
context.startActivity(intent);
}
});
}
}
Sends data about Department position to DepartmentDeatail activity
public class DeptDetail extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dept_detail);
Bundle extra = getIntent().getExtras();
deptpos = Integer.parseInt(extra.getString("Department"));
.........
public void onClick(View v) {
int id= v.getId();
Intent in;
Bundle extras = new Bundle();
in = new Intent(DeptDetail.this, Mission.class);
extras.putString("Mission",mission[deptpos]);
extras.putString("Deptid", deptpos+"");
in.putExtras(extras);
startActivityForResult(in,1);
}
}
and DeptDetail activity sends same Deptpos to its child activity Mission
public class Mission extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mission);
Bundle extra = getIntent().getExtras();
String mission = extra.getString("Mission");
deptid=Integer.parseInt(extra.getString("Deptid"));
TextView txtmission = (TextView)findViewById(R.id.txtmission);
try {
txtmission.setText(mission);
}
catch (NullPointerException e)
{
txtmission.setText("");
}
}
}
And now I want same Deptid to be accessed in DeptDetail activity which always calls for intent from Department activity, which is not available as usual..
So please show me the way to pass the data to child and back to parent.
I tried
onActivityResult(..)
but it wasn't called before onCreate where extra is read and generating NullPointerException
This question already has answers here:
How do I get extra data from intent on Android?
(16 answers)
Closed 8 years ago.
I just want to show an simple Employee record on next layoutPage/Activity !!
this is my EmpLogin Java File
public class EmpLogin extends Activity {
private Button show;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// TODO Auto-generated method stub
show=(Button)findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText no=(EditText)findViewById(R.id.getno);
EditText name=(EditText)findViewById(R.id.getname);
EditText sal=(EditText)findViewById(R.id.getsalary);
Intent emp = new Intent(getApplicationContext(),EmpShow.class);
emp.putExtra("EmpNO",(no.getText().toString()));
emp.putExtra("EmpName",(name.getText().toString()));
emp.putExtra("Sal",(sal.getText().toString()));
startActivity(emp);
}
});
}
}
How to use Retrieve data from the Intent ??? By using getExtra() method ??? or there is simple way ?? this my EmpShow.class file !!
public class EmpShow extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empshow);
// TODO Auto-generated method stub
Intent show = getIntent();
}
}
As you passed data insideintent, So try to fetch the intent which has started your activity using the getIntent() method:
Intent intent = getIntent();
If your extra data is represented as strings, then you can use
intent.getStringExtra(String name) method.
As per your OP's class and intent. Here is the snipped which can fetch the values from your intent against respective variable,
public class EmpShow extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.empshow);
Intent intent = getIntent();
String empNo = intent.getStringExtra("EmpNO");
String empNname = intent.getStringExtra("EmpName");
String empSal = intent.getStringExtra("sal");
}
}
First Activity:
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
i.putExtra("VAL",value1);
startActivity(i);
Second Activity:
Intent getI=getIntent();
String a =getI.getStringExtra("VAL");