使用Intent接收数据 [英] Receiving data using Intent

查看:174
本文介绍了使用Intent接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Intent从我的NoteActivity向MainActivity类的方法(addNote())发送一些数据

I am trying to send some data using Intent from my NoteActivity to MainActivity class's method (addNote())

 public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;
    ArrayList<String> notes = new ArrayList<String>();





    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);
    notes.add("sql");

    //Intent intent = new Intent(NotePad.this,MainActivity.class);
    //intent.putExtra(EXTRA_MESSAGE,str);
    //startActivity(intent); 

}

void addNote()
{

    Intent intent = this.getIntent();
    String title = intent.getStringExtra(NotePad.EXTRA_MESSAGE).toString();
    notes.add(title);
    Log.d("Data","inside saveData");
    Log.d("Data","title: "+title);

   }

 }



 NoteActivity
 public class NotePad extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "key";

ArrayList<String> data = new ArrayList<String>();


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

    final FloatingActionButton button = (FloatingActionButton)findViewById(R.id.save_button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           SaveData();
        }
    });
}

public void SaveData()
{
    FileOutputStream outputStream;

    try {
        ***Some code***

        SendData();
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

public void SendData()
{
    EditText editText = findViewById(R.id.textView3);

  final String str = editText.getText().toString();

    Intent intent = new Intent(NotePad.this,MainActivity.class);
    intent.putExtra(EXTRA_MESSAGE,str);
    startActivity(intent);

  }
 }

当我在onCreate()方法中将(str)接收为(title)时,一切正常,并且我收到数据,但是当我尝试在addNote()方法中接收Intent时,我没有收到Intent数据

When I am reciving the (str) in my onCreate() method as(title) every thing works fine and I receive the data, but when I try to receive the Intent in my addNote() method I dont receive the Intent data

推荐答案

从onCreate方法调用方法addNote().

Call your method addNote() from onCreate method.

   public class MainActivity extends AppCompatActivity {

        private RecyclerView recyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager layoutManager;
        ArrayList<String> notes = new ArrayList<String>();





        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        notes.add("sql");

        //Intent intent = new Intent(NotePad.this,MainActivity.class);
        //intent.putExtra(EXTRA_MESSAGE,str);
        //startActivity(intent); 

          addNote();

    }

    void addNote()
    {

        Intent intent = this.getIntent();
        String title = intent.getStringExtra(NotePad.EXTRA_MESSAGE).toString();
        notes.add(title);
        Log.d("Data","inside saveData");
        Log.d("Data","title: "+title);

       }

     }

这篇关于使用Intent接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆