如何将数据从edittext传输到微调器都处于不同的活动中

时间:2016-09-20 15:28:51

标签: android

我有3个活动。在第1个活动中我有2个按钮第1个按钮是开始第2个活动而第2个按钮是开始第3个活动。第2个活动包含edittext和按钮。第3个活动包含Spinner.I想转移从edittext到spinner的数据

1 activity

2 activity

[3活动] [3]

第一项活动

public class MainActivity extends Activity {
String item;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String resultStr = data.getStringExtra("result");
            item=resultStr;
        }
    }
}

public void start1(View view)
{
    Intent intent = new Intent(this,Task.class);
    startActivityForResult(intent, 1);
}
public void start2(View view)
{
    Intent intent = new Intent(getBaseContext(), Spinner.class);
    intent .putExtra("send_data",item);
    startActivity(intent);
}
}

第二项活动:

public class Task extends Activity {
Button btnAdd;
EditText subject;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_task);
    btnAdd = (Button)findViewById(R.id.add) ;


    btnAdd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            subject = (EditText) findViewById(R.id.editText);
            String content = subject.getText().toString();
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result",content);
            setResult(Activity.RESULT_OK,returnIntent);
        }
    });
}

}

第三项活动:

public class Spinner extends Activity {
android.widget.Spinner spinner;
ArrayAdapter<String> dataAdapter;
ArrayList<String> list = new ArrayList<String>();
SharedPreferences sharedPref;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner);
    spinner = (android.widget.Spinner) findViewById(R.id.spinner);


    sharedPref = getSharedPreferences("Info", Context.MODE_PRIVATE);
    Set<String> savedStringSet = sharedPref.getStringSet("SpinnerData", null);
    if (savedStringSet != null) {
        list = new ArrayList<>(savedStringSet);
    }

    dataAdapter = new ArrayAdapter<String>(Spinner.this, android.R.layout.simple_spinner_item, list.toArray(new String[list.size()]));
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);
    spinner.setPrompt("SELECT AN ITEM");
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapter, View v,
                                   int position, long id) {



        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {


        }
    });

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("send_data");
        list.add(value);
        dataAdapter = new ArrayAdapter<String>(Spinner.this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putStringSet("SpinnerData", new HashSet<String>(list));
    editor.apply();
}

错误:

   java.lang.NullPointerException
                                                                                 at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
                                                                              at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
                                                                              at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
                                                                              at android.widget.Spinner.onMeasure(Spinner.java:429)
                                                                              at android.view.View.measure(View.java:15172)
                                                                              at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
                                                                              at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
                                                                              at android.view.View.measure(View.java:15172)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                              at android.view.View.measure(View.java:15172)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
                                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
                                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
                                                                              at android.view.View.measure(View.java:15172)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                              at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
                                                                              at android.view.View.measure(View.java:15172)
                                                                              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
                                                                              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
                                                                              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
                                                                              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
                                                                              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
                                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                                                                              at android.view.Choreographer.doFrame(Choreographer.java:525)
                                                                              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                                                                              at android.os.Handler.handleCallback(Handler.java:615)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                              at android.os.Looper.loop(Looper.java:137)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:511)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                              at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

第一部分是将第二项活动的结果收回第一项活动。

为此,我拿了this SO answer。你可以在下面阅读。

  

FirstActivity致电SecondActivity使用   startActivityForResult()方法

     

例如:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
     

SecondActivity中设置要返回的数据   FirstActivity。如果您不想返回,请不要设置任何。

     

例如:在secondActivity中,如果要发回数据:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
     

如果您不想返回数据:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
     

现在在您的FirstActivity类中编写以下代码   onActivityResult()方法。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult

第二部分是通过将这些数据从第一个活动传递到第三个活动来完成的。

您可以通过执行以下操作来执行此操作:

Intent intent = new Intent(getBaseContext(), ThirdActivity.class);
intent .putExtra("key",result);
startActivity(intent);

在第3项活动中你可以使用:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
    //The key argument here must match that used in the other activity
}

希望这有帮助

答案 1 :(得分:0)

您可以使用onActivityResult完成第二个将edittext数据传递给第一个活动的活动。 然后,您可以启动第三个将edittext数据传递给它的活动。 然后在您的第三个活动中,您可以填充您的微调数据。

如果我可以建议你,微调器是一个旧的api,让我们在对话片段中使用更好的列表