简体   繁体   English

在按钮上单击,RecyclerView仅显示第一项

[英]On buttonclick the RecyclerView shows only the first item

I have 2 Activities, first to send data and the other pastes this data in a RecyclerView. 我有2个活动,第一个活动是发送数据,另一个活动是将该数据粘贴到RecyclerView中。

My problem is when I press on the button to take the data from the EditTexts and move them to the RecyclerView, it only updates the RecyclerView. 我的问题是,当我按下按钮以从EditTexts中获取数据并将其移至RecyclerView时,它只会更新RecyclerView。 I mean it shows only the first item which was put before, but when I try to add more data to the RecyclerView it overwrites them in the first item. 我的意思是,它仅显示放置在前面的第一项,但是当我尝试向RecyclerView添加更多数据时,它将在第一项中覆盖它们。 Here are my activities and layouts: 这是我的活动和布局:

The Sender Activity 发件人活动

btn_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etTitle.length() != 0 || etDes.length() != 0){
                String titled = etTitle.getText().toString();
                String desed = etDes.getText().toString();
                Bundle bund = new Bundle();
                bund.putString("title", titled);
                bund.putString("des", desed);
                Intent inte = new Intent(getApplicationContext(), MainActivity.class);
                inte.putExtras(bund);
                startActivity(inte);
            }else {
                Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
            }
        }
    });

The Receiver Activity: 接收者活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), DataInput.class);
            startActivity(intent);
        }
    });

    recyclerView = findViewById(R.id.rv);

    dAdapter = new DataAdapter(dataList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(dAdapter);

    bundle = getIntent().getExtras();
    if (bundle != null) {
        sendData();
    }
}

private void sendData() {
    String addedTitle = bundle.getString("title");
    String addedDes = bundle.getString("des");
    Data data = new Data(addedTitle, addedDes);
    dataList.add(data);
    dAdapter.notifyDataSetChanged();
}

My XML Files: 我的XML文件:

activity_main.xml: activity_main.xml中:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/pen2"
    tools:context=".MainActivity">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        app:backgroundTint="#a40038"
        app:srcCompat="@mipmap/add" />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rv">
    </android.support.v7.widget.RecyclerView>

list_item.xml: list_item.xml:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="#606c6c6c"
    android:layout_margin="10dp"
    app:cardElevation="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_title"
            android:textSize="25sp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:fontFamily="@font/mohave"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="15dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_des"
            android:textSize="20sp"
            android:textColor="#fff"
            android:fontFamily="@font/mohave"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginBottom="10dp"/>
    </LinearLayout>

When you come back to your Sender activity and then again go to the receiver activity, you are sending data in a bundle (A fresh bundle). 当您返回到“发件人”活动,然后再次转到“接收者”活动时,您是以捆绑(新捆绑)形式发送数据。 This bundle doesn't have any older data and when the receiver activity receives data from this bundle, it creates a new data (the previous data is being erased with a new data (sent from the sender) every time). 该捆绑包没有任何较旧的数据,并且当接收方活动从该捆绑包中接收数据时,它会创建一个新数据(每次都使用新数据(从发送方发送)擦除以前的数据)。 You need to store your previous data when you come back to your Sender activity. 回到“发件人”活动时,您需要存储以前的数据。

One way to achieve this is to store the receiver's data locally in a database (A bad practice). 实现此目的的一种方法是将接收方的数据本地存储在数据库中(一种不良做法)。 What you can do is in your Sender activity, use 您可以在发件人活动中使用

startActivityForResult()

instead of 代替

startActivity()

and override the onBackPressed() in your receiver activity. 并在您的接收器活动中覆盖onBackPressed()。 and onBackPressed(), send the 'data' variable back to your sender activity and append that data with the new data while sending it again to the receiver activity. 和onBackPressed(),将“数据”变量发送回您的发送者活动,并在将新数据附加到该数据之后,再次将其发送到接收者活动。

Or every time you send your bundle, instead of sending a string, send an array of strings (Having previous data). 或者,每次发送捆绑包时,不发送字符串,而是发送字符串数组(具有先前的数据)。 (Simplest solution) (最简单的解决方案)

Please let me know if you need any help with the code. 如果您需要任何有关代码的帮助,请告诉我。

Change the position of this code 更改此代码的位置

bundle = getIntent().getExtras();
if (bundle != null) {
    sendData();
}

to above , before initializing and setting the adapter. 以上,在初始化和设置适配器之前。 And save your list , before leaving the activity to save the past values. 并保存列表,然后再离开活动以保存过去的值。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM