[英]On buttonclick the RecyclerView shows only the first item
我有2个活动,第一个活动是发送数据,另一个活动是将该数据粘贴到RecyclerView中。
我的问题是,当我按下按钮以从EditTexts中获取数据并将其移至RecyclerView时,它只会更新RecyclerView。 我的意思是,它仅显示放置在前面的第一项,但是当我尝试向RecyclerView添加更多数据时,它将在第一项中覆盖它们。 这是我的活动和布局:
发件人活动
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();
}
}
});
接收者活动:
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();
}
我的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:
<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>
当您返回到“发件人”活动,然后再次转到“接收者”活动时,您是以捆绑(新捆绑)形式发送数据。 该捆绑包没有任何较旧的数据,并且当接收方活动从该捆绑包中接收数据时,它会创建一个新数据(每次都使用新数据(从发送方发送)擦除以前的数据)。 回到“发件人”活动时,您需要存储以前的数据。
实现此目的的一种方法是将接收方的数据本地存储在数据库中(一种不良做法)。 您可以在发件人活动中使用
startActivityForResult()
代替
startActivity()
并在您的接收器活动中覆盖onBackPressed()。 和onBackPressed(),将“数据”变量发送回您的发送者活动,并在将新数据附加到该数据之后,再次将其发送到接收者活动。
或者,每次发送捆绑包时,不发送字符串,而是发送字符串数组(具有先前的数据)。 (最简单的解决方案)
如果您需要任何有关代码的帮助,请告诉我。
更改此代码的位置
bundle = getIntent().getExtras();
if (bundle != null) {
sendData();
}
以上,在初始化和设置适配器之前。 并保存列表,然后再离开活动以保存过去的值。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.