简体   繁体   中英

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

this is my Activity2.javacode where i took inputs for sorting

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

              }

this for marge sort

            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();


          }


    }
}

在此处输入图片说明

this my bubbleSortActivity.java code

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);

            }
    }

在此处输入图片说明

In your Activity1

public void onclickButton(View V){
Intent intent = new Intent(this,Activity2.class);
i.putStringArray("Extra_message", strArr);
startActivity(intent);
}

You have receive this String Array in Activity 2

Intent  intent = getIntent().getExtras(); 
String[] bubble = intent.getStringArray("Extra_message");

You need to pass EditText data from Activity2 to BubbleSortActivity

Intent i = new Intent(this, BubbleSortActivity.class);
i.putExtra("Extra_message", e1.getText().toString());      
startActivity(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM