简体   繁体   English

当我第二次将数据从另一个活动发送到MainActivity时,我获得了我第一次获得的数据

[英]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.

相关问题 我第一次注销后如何获得第二次 OTP? - How do i get an OTP the second time after I've logged out the first time? 我第一次从数据中获取信息时,Android如何去其他活动 - Android how to go to the other activity the first time I get information from data 如何从“设置”活动中获取字符串数据并将其发送到另一个活动? - How do I get string data from Settings activity and send it to another activity? 如何从 Settings Activity 修改 MainActivity 上的数据? - How do i modify data on my MainActivity from Settings Activity? 我如何使用Mule Java中的相同打开套接字从第二次发送接收数据? - how i can send a receive a data from second time using the same open socket in mule java? 我需要不断地将数据从一个活动发送到Android中的另一个活动 - I need to constantly send data from one activity to another in Android 如何将字符串数据从sqlite发送到另一个活动 - How do I send String data from sqlite to another activity 我得到了 listView 中每个项目的密钥并将其保存到 Firebase 我想将该密钥发送到另一个活动,但每次我得到相同的密钥 - I got key for every item in the listView and its saved to Firebase I want to send that key to another activity but every time I got the same key 第一次离开应用程序后,我的数据会保存,但第二次后,它不保存 - My data saves after I leave the app the first time, but after the second time, it doesn't save 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM