在MainActivity中,我有一个CardView项目的RecyclerView列表。
单击任何CardView会启动DetailsActivity CardView,以显示来自数据库的更多数据,用于特定的CardView。
点击DetailsActivity CardView启动EditActivity以编辑CardView的数据。单击“保存”按钮可将用户返回到DetailsActivity,以便用户可以看到对该CardView的任何已编辑更改。 CardView Item的模型实现了Parcelable,因此我使用getParcelable()来传递项目及其在Activity Intents中的数据库数据。
我的问题是,点击“保存”后,用户返回到DetailsActivity CardView,并显示CardView的旧视图(用户刚刚输入并保存到数据库的更新数据之前的视图)。我试图使用“adapter.notifyDataSetChanged()”来更新View,但没有运气。
从EditActivity,我需要重新加载MainActivity,以便RecyclerView更新Adapter,然后将用户返回到更新的DetailsActivity CardView。我可以以某种方式使用startActivityForResult()吗?我在这里缺少什么?
以下是UI进展:
MainActivity RecyclerView和适配器:
- Adapter使用recyclerItemClickListener将点击的CardView项目发送回MainActivity,这样我就可以使用onItemClick()来启动DetailsActivity。
- onItemClick()通过Intent
启动DetailsActivityDetailsActvity:
- 使用onClickListener和Intent启动EditActivity
EditActivity:
- 我想点击“保存”按钮将新的/更新的CardView数据保存到数据库,并将用户返回到详细信息活动,以便用户可以看到对CardView的更改。
Adapter
...
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);
final ItemHolder itemHolder = new ItemHolder(view);
// Attach a Click listener to the items's (row) view.
// itemView is from the ItemHolder().
// onItemClick is the click method in MainActivity.
itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
// Handles the row being clicked.
@Override
public void onClick(View view) {
ListItem adapterItem = MyRecylerAdapter.this.getItem(itemHolder.getAdapterPosition());
if (recyclerItemClickListener != null) {
// pass the item to the Main Activity
// through the RecyclerItemClickListener file and its
// public interface.
recyclerItemClickListener.onItemClick(itemHolder.itemView, adapterItem);
}
}
});
return itemHolder;
}
MainActivity (the RecyclerView Activity)
...
@Override
public void onItemClick(View view, ListItem listItem) {
Intent intent = new Intent(this, DetailsActivity.class);
intent.putExtra("item",listItem);
startActivity(intent);
}
DetailsActivity
...
public class DetailsActivity extends AppCompatActivity {
onCreate(...) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
listItem = extras.getParcelable("item");
}
helper = new SQLiteDB(this);
listItems = new ArrayList<>();
listItems = helper.getAllDBItems();
cvdcardType1 = (TextView) findViewById(R.id.cvdcardType1);
if (listItems !=null && listItems.size() !=0) {
cvdcardType1.setText(listItem.getType());
}
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(DetailsActivity.this,EditActivity.class);
i.putExtra("cvdItem",listItem);
startActivityForResult(i,REQUEST_CODE);
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
listItem = null;
Bundle returnExtras = getIntent().getExtras();
if (returnExtras != null) {
listItem = returnExtras.getParcelable("returnItem");
if (listItems !=null && listItems.size() !=0) {
cvdcardType1.setText(listItem.getType());
...
}
}
}
else if (resultCode == Activity.RESULT_CANCELED) {
// Finish the startActivityForResult().
finish();
}
}
}
}
EditActivity
public class EditActivity extends AppCompatActivity {
onCreate(...) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
item = extras.getParcelable("cvdItem");
}
}
// "Save Button"
public void onClickSaveEdits(View v) {
// update database
// update RecyclerView adapter and CardView Items
// update DetailsActivity CardView
// return the user to DetailsActivity to see updated CardView
Intent returnIntent = new Intent(EditActivity.this, DetailsActivity.class);
returnIntent.putExtra("returnItem", item);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
答案 0 :(得分:0)
您必须在转向EditActivity时完成DetailActivity,然后在onBackPressed()方法中再次启动DetailActivity,如下所示:
Intent intent = new Intent(EditActivity.this, DetailsActivity.class);
intent.putExtra("item",item);
startActivity(intent);
答案 1 :(得分:0)
只需在编辑详细信息后再次在MainActivity中的
public static void ProcessQueueMessage([QueueTrigger(AppConstants.AzureBlobQueue)] string message, TextWriter log, ExecutionContext context) { try { //Do Something log.WriteLine(message); } catch (Exception ex) { if(message.DequeueCount == 1) { //Logic 1 to notify } if(message.DequeueCount == 2) { //Logic 2 to notify } if(message.DequeueCount == 3) { //Logic 3 to notify } if(message.DequeueCount == 4) { //Logic 4 to notify } if(message.DequeueCount == 5) { //Logic 5 to notify } } }
中读取数据库项目。
onResume()
简单来说,再次从数据库中读取列表并重新绘制它。 并确保您在 EditActivity
中保存了数据库中的更改并使用allList = sqLiteDB.getAllDBItems();
adapter.notifyDatasetChanged();
调用您的 EditActivity ,无论您更改了什么,都会将其传回去
的setResult();
并在DetailActivity的 onActivityResult 方法中使用新值填充数据。