![](/img/trans.png)
[英]How to return data from activity2 to activity1 if I don't know when activity2 will end?
[英]i have taken inputs(number) from one activity(Activity2) and now i want show the passes in another activity(BubbleSortActivit)but this is not showing
这是我的 Activity2.javacode,我在其中输入进行排序
package com.example.jabed.algorithmsimulator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Activity2 extends AppCompatActivity {
private Button button,quicksort ;
private TextView t1;
private EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
t1 =(TextView) findViewById(R.id.textView2);
}
public void onclickButton(View V){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void onclickButton2(View V){
try {
EditText e1 = (EditText) findViewById(R.id.editText2);
t1 =(TextView) findViewById(R.id.textView2);
button = (Button) findViewById(R.id.button1);
quicksort = (Button) findViewById(R.id.button2);
if (V.getId() == R.id.button2) {
Intent intent1 = new Intent(Activity2.this, QuicksortActivity.class);
startActivity(intent1);
Toast.makeText(Activity2.this, "You have selected Quick Sort ", Toast.LENGTH_SHORT).show();
}
> **`when i push bubblesort button it will do this in the bellow section`**
if (V.getId() == R.id.button3) {
e1 = (EditText) findViewById(R.id.editText2);
t1 =(TextView) findViewById(R.id.textView2);
String text = e1.getText().toString();
Intent intent = new Intent(this, BubbleSortActivity.class);
intent.putExtra("Extra_message",text);
startActivity(intent);
Toast.makeText(Activity2.this, "You have selected Bubble Sort ", Toast.LENGTH_SHORT).show();
// input ase
}
这是玛吉排序
if (V.getId() == R.id.button4) {
Intent intent = new Intent(this, MargeSortActivity.class);
startActivity(intent);
Toast.makeText(Activity2.this, "You have selected Marge Sort ", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(Activity2.this,"number please",Toast.LENGTH_LONG).show();
}
}
}
这是我的 bubbleSortActivity.java 代码
package com.example.jabed.algorithmsimulator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class BubbleSortActivity extends AppCompatActivity {
private Button button;
private TextView t2;
private EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bubble_sort);
> **taken input from Activity2.xml though editText**
Intent intent = getIntent();
String bubble = intent.getStringExtra("Extra_message");
t2 =(TextView)findViewById(R.id.textView2);
> **putting it an array by sparating with space in the input section**
String [] intString=bubble.split(" ");
**in bellow section is my sorting code just**
int bubbleArr[]=new int[intString.length];
for(int i=0;i<bubbleArr.length;i++) {
bubbleArr[i]=Integer.parseInt(intString[i]);
}
int n = bubbleArr.length;enter link description here
t2.setText(" Passes in ascending order" + "\n");
for (int i = 0; i < n-1; i++) {
boolean swap=false;
for (int j = 0; j < n-i-1; j++) {
if (bubbleArr[j] > bubbleArr[j+1])
{
int temp = bubbleArr[j];
bubbleArr[j] = bubbleArr[j+1];
bubbleArr[j+1] = temp;
swap=true;
}
}
if(swap==false)
break;
> **here i want to show all the passing**
String []strArr=new String[bubbleArr.length];
t2.setText(" Pass "+(i+1)+": ");
for (int k=0; k<n; k++) {
strArr[k]=String.valueOf(bubbleArr[k]);
t2.setText(strArr[k]+" ");
}
t2.setText("\n");
}
}
public void onclickButton(View V){
Intent intent = new Intent(this,Activity2.class);
startActivity(intent);
}
}
在您的活动 1
public void onclickButton(View V){
Intent intent = new Intent(this,Activity2.class);
i.putStringArray("Extra_message", strArr);
startActivity(intent);
}
您已在活动 2 中收到此字符串数组
Intent intent = getIntent().getExtras();
String[] bubble = intent.getStringArray("Extra_message");
您需要将 EditText 数据从Activity2
传递给BubbleSortActivity
Intent i = new Intent(this, BubbleSortActivity.class);
i.putExtra("Extra_message", e1.getText().toString());
startActivity(i);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.