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

时间:2017-05-30 00:59:51

标签: android android-intent android-edittext android-recyclerview android-cardview

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

当用户保存任何已编辑的数据时,EditActivity将关闭,用户将返回到特定的CardView。但是CardView没有使用编辑的数据进行更新。 RecycViewView的CardViews列表确实按预期更新,因为如果我退出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?**
}

1 个答案:

答案 0 :(得分:1)

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