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