0

I have 2 Activities, first to send data and the other pastes this data in a 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. 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. 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:

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:

<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>

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • You should need to create a database. Look at it this sample app [Room](https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0) – Krishna Sharma Aug 07 '18 at 18:28

2 Answers2

0

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. 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.

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.

Jaswant Singh
  • 9,900
  • 8
  • 29
  • 50
  • I don't know how to use startActivityForResult() in a proper way – AbdallahBedo Aug 08 '18 at 10:47
  • You can find a detailed explanation on this thread https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – Jaswant Singh Aug 08 '18 at 10:48
  • Using a database for such tasks is never a good idea as the data is not needed after the application is closed and using database will increase your cache size for no reason. If you don't need that data, don't forget to clear your database on onDestroy() of the sender activity. – Jaswant Singh Aug 10 '18 at 04:22
  • Great .. but I actually need the data after closing the app because it is (To Do List) app. Thanks so much for your help. – AbdallahBedo Aug 11 '18 at 05:08
0

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.

Kaveri
  • 1,060
  • 2
  • 12
  • 21