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>