[英]When i send data from another activity to MainActivity for the second time, i get data what i've already got for the first time
I develop a simple app with cards with 2 textViews. 我开发了一个带有2个textViews卡片的简单应用程序。 Have just 2 activities.
只有2项活动。 On the second activity i put data and send it to MainActivity for showing it on the card.
在第二个活动中,我放入数据并将其发送到MainActivity以在卡上显示。 When i create the first card with data, everything good, but if i want to create another one with another data, i get on MainActivity exactly the same card with data from the first time.
当我用数据创建第一张卡片时,一切都很好,但如果我想用另一个数据创建另一张卡片,我会在MainActivity上使用第一次带有数据的卡片。 ` MainActivity:
`MainActivity:
public class MainActivity extends ActionBarActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
public ArrayList<DataObject> dataObject;
FloatingActionButton fab;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataObject = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerAdapter(dataObject);
recyclerView.setAdapter(adapter);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddNewCard.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data==null)
return;
String title = data.getStringExtra("title");
String text = data.getStringExtra("text");
dataObject.add(i, new DataObject(title, text));
i++;
}
}
And the code of the second activity: 和第二个活动的代码:
public class AddNewCard extends ActionBarActivity {
TextView mTitle, mText;
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_card);
initElements();
}
private void initElements() {
mTitle = (TextView) findViewById(R.id.editTitle);
mText = (TextView) findViewById(R.id.editGoal);
mButton = (Button) findViewById(R.id.sendButton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("title", mTitle.getText().toString());
intent.putExtra("text", mText.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
});
}
}
Also i have DataObject class with 2 Strings, constructor and getters and recycler adapter which works fine. 另外,我有带有2个字符串的DataObject类,构造函数和getter以及回收器适配器,它们工作正常。
An easier, faster, but not stronger way to pass data between activities is to use a public static classs with static variables. 在活动之间传递数据的更简单,更快速但不强的方法是使用带有静态变量的公共静态类。 As I said before, this is maybe not the right way to do it, but it works.
正如我之前所说,这可能不是正确的方法,但它确实有效。 Is not rocket science, but it is very useful if you create small projects.
不是火箭科学,但如果你创建小项目它是非常有用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.