我有一个CardViews的RecyclerView列表,当用户使用一些EditTexts输入一堆数据时,它就会创建。单击列表中的单个CardView将加载一个仅向用户显示该特定CardView的DetailsActivity。再次单击该CardView将加载一个EditActivity,允许用户编辑他们输入的原始数据。
当用户保存任何已编辑的数据时,EditActivity将关闭,用户将返回到特定的CardView。但是CardView不会使用编辑后的数据进行更新。CardViews的RecyclerView列表确实会按预期更新,因为如果我退出DetailsActivity并返回到MainActivity,那么编辑过的CardView就会正确地显示出来。如何刷新DetailsActivity中单个CardView的视图?
MainActivity (the RecyclerView list)
...
@Override
public void onItemClick(int position, final View view) {
// Create a new intent to send data from this MainActivity to the DetailsActivity
Intent intent = new Intent(this,CardViewDetails.class);
// Send the position of the CardView item that was clicked on in the intent.
intent.putExtra("position",position);
startActivity(intent);
}
DetailsActivity (for the single CardView)
...
// Get the position of the clicked on RecyclerView list CardView from
// the MainActivity's intent bundle.
Bundle extras = getIntent().getExtras();
if (extras != null) {
// get the CardView item using the int position from the
// MainActivity's onItemClick() and the putExtra in the intent.
position = extras.getInt("position", 0); // 0 is default value
}
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// pass the position variable to the start method of
// EditActivity so get the correct data from the db can
// be loaded onto the EditText lines, etc. of the
// EditActivity and that can then be used to pass the
// data back to the MainActivity.
EditActivity.start(CardViewDetails.this,listItems.get(position));
}
});
EditActivity (for editing the original CardView data)
...
// Launches the activity to edit an Item (CardView) that was clicked on from the
// RecyclerView list in the MainActivity file. The intent brings the item's
// position in the RecyclerView Adapter so the correct Item is edited.
public static void start(Context context, ListItem item) {
Intent intent = new Intent(context, ActActivity.class);
// From the OnItemClick method in the MainActivity the RecyclerView item
// position from the Adapter is passed into a putExtra bundle that the
// intent carries to this Activity. The data is then copied in the onCreate()
// below using getIntent().getParcelableExtra(). So this is to update an
// existing CardView item.
intent.putExtra(ActActivity.class.getSimpleName(),item);
context.startActivity(intent);
}
public void onClickSaveEdits(View v) {
// Update the user EditText input to the database.
sqLiteDB.update(item);
// Close the EditActivity.
finish();
**what am I missing here to update/refresh the view for the just edited CardView that is shown in the DetailsActivity?**
}
发布于 2017-05-30 09:06:46
无论何时对用于RecyclerView的数据进行更改,都可以在适配器上调用notifyDataSetChanged()
。
例如,您的onClickSaveEdits(View v)
方法将如下所示:
public void onClickSaveEdits(View v) {
// Update the user EditText input to the database.
sqLiteDB.update(item);
adapterObjectHere.notifyDataSetChanged();
// Close the EditActivity.
finish();
}
https://stackoverflow.com/questions/44251965
复制相似问题