繁体   English   中英

Android:如何刷新视图以获取更新的CardView?

[英]Android: how do I refresh view for an updated CardView?

当用户使用一些EditText输入一堆数据时,我创建了CardViews的RecyclerView列表。 在列表中单击单个CardView会加载DetailsActivity,该DetailsActivity仅向用户显示该特定CardView。 再次单击该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?**
}

每当您更改用于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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM