猿问

如何从一个活动传递数据,然后在另一个活动的 recyclerview 中检索?

我想将编辑后的文本值从一个活动传递到另一个活动,然后在 recyclerView 中显示。

可能我必须把值放在等式标记下?但我不知道如何将编辑后的文本值放入其中。


我想从这个类传递数据


public class addEvent extends AppCompatActivity {


public void addEvent(View view){


    EditText timeEditText = findViewById(R.id.timeEditText);

    EditText descriptionEditText = findViewById(R.id.descriptionEditText);


    Intent intent = new Intent(getApplicationContext(), events.class);

    String timeInfo = timeEditText.getText().toString();

    String descriptionInfo = descriptionEditText.getText().toString();

    intent.putExtra("time", timeInfo );

    intent.putExtra("description", descriptionInfo);


}


public void events(View view){


    Intent intent1 = new Intent(getApplicationContext(), events.class);

    startActivity(intent1);

}




@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_add_event);

}

}


慕哥9229398
浏览 219回答 3
3回答

HUWWW

您想使用“getStringExtra”,而不是“getExtras/bundle”

慕桂英4014372

您必须从定义附加功能的同一范围内调用 startActivity:public void addEvent(View view){    EditText timeEditText = findViewById(R.id.timeEditText);    EditText descriptionEditText = findViewById(R.id.descriptionEditText);    Intent intent = new Intent(getApplicationContext(), events.class);    String timeInfo = timeEditText.getText().toString();    String descriptionInfo = descriptionEditText.getText().toString();    intent.putExtra("time", timeInfo );    intent.putExtra("description", descriptionInfo);    startActivity(intent);//add this}

元芳怎么了

在您的 addEvent() 方法中,public void addEvent(View view){&nbsp; &nbsp; //your code&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(addEvent.this, events.class);&nbsp; &nbsp; &nbsp; &nbsp; Bundle bundle = new Bundle();&nbsp; &nbsp; &nbsp; &nbsp; bundle.putString("time", timeInfo);&nbsp; &nbsp; &nbsp; &nbsp; bundle.putString("description",descriptionInfo);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtras(bundle);&nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);}然后,在您的事件活动和 onCreate() 方法中,&nbsp; &nbsp; Bundle bundle = getIntent().getExtras();&nbsp; &nbsp; String time = bundle.getString("time");&nbsp; &nbsp; String description = bundle.getString("description");&nbsp; &nbsp; ArrayList<ListItem> arrayList = new ArrayList<>();&nbsp; &nbsp; ListItem listItem = new ListItem(time, description);&nbsp; &nbsp; arrayList.add(listItem);&nbsp; &nbsp; adapter = new MyAdapter(this, arrayList);&nbsp; &nbsp; recyclerView.setAdapter(adapter);这将完成您的工作。
随时随地看视频慕课网APP

相关分类

Python
我要回答