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.
Related
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 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.
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");
}
}
I don't know why, but this isn't working. Can anyone see anything wrong with my code.
I have two activities and I'm passing data from two
public class InputActivity extends AppCompatActivity {
EditText number1EditText;
EditText number2EditText;
Button addButton;
InputActivity code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
number1EditText = (EditText)findViewById(R.id.editText);
number2EditText = (EditText)findViewById(R.id.editText2);
addButton = (Button)findViewById(R.id.button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(getApplicationContext(), AddActivity.class);
mIntent.putExtra("number1", number1EditText.getText().toString());
mIntent.putExtra("number2", number2EditText.getText().toString());
startActivity(mIntent);
}
});
}
AddActivity code looks like this
public class AddActivity extends AppCompatActivity {
TextView answer;
double y=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
answer =(TextView)findViewById(R.id.textView);
String value1 = getIntent().getExtras().getString("number1");
String value2 = getIntent().getExtras().getString("number2");
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
}
Just looking quickly:
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
You're passing a int to the method setText() which happens to have an overload that receives a int argument for the cases when you pass the reference from a String in some xml. You may be getting a ResourceNotFoundException.
If you want to show a text with the sum between your values:
answer.setText(String.valueOf((int) (Double.parseDouble(value1)+Double.parseDouble(value2))));
Just keep in mind that there are a lot of checks you have to do first, you may get another exceptions doing this kind of parse, like NumberFormatException for instance.