首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓:如何刷新更新后的CardView的视图?

安卓:如何刷新更新后的CardView的视图?
EN

Stack Overflow用户
提问于 2017-05-30 08:59:51
回答 1查看 2.4K关注 0票数 0

我有一个CardViews的RecyclerView列表,当用户使用一些EditTexts输入一堆数据时,它就会创建。单击列表中的单个CardView将加载一个仅向用户显示该特定CardView的DetailsActivity。再次单击该CardView将加载一个EditActivity,允许用户编辑他们输入的原始数据。

当用户保存任何已编辑的数据时,EditActivity将关闭,用户将返回到特定的CardView。但是CardView不会使用编辑后的数据进行更新。CardViews的RecyclerView列表确实会按预期更新,因为如果我退出DetailsActivity并返回到MainActivity,那么编辑过的CardView就会正确地显示出来。如何刷新DetailsActivity中单个CardView的视图?

代码语言:javascript
运行
复制
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?**
}
EN

回答 1

Stack Overflow用户

发布于 2017-05-30 09:06:46

无论何时对用于RecyclerView的数据进行更改,都可以在适配器上调用notifyDataSetChanged()

例如,您的onClickSaveEdits(View v)方法将如下所示:

代码语言:javascript
运行
复制
public void onClickSaveEdits(View v) {
    // Update the user EditText input to the database.
    sqLiteDB.update(item);
    adapterObjectHere.notifyDataSetChanged();

    // Close the EditActivity.
    finish();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44251965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档