RecyclerView:如何刷新Details Activity? [英] RecyclerView: how to refresh Details Activity?

查看:82
本文介绍了RecyclerView:如何刷新Details Activity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MainActivity中,我有一个CardView项目的RecyclerView列表.

In MainActivity I have a RecyclerView list of CardView Items.

单击任何CardView都会启动DetailsActivity CardView,该CardView显示针对该特定CardView的数据库中的更多数据.

A click on any CardView launches a DetailsActivity CardView that show more data from the database, for that specific CardView.

单击DetailsActivity CardView,将启动EditActivity以编辑该CardView的数据.单击保存"按钮将使用户返回到DetailsActivity,以便用户可以看到对该CardView进行的任何编辑更改. CardView Item的模型实现了Parcelable,因此我使用getParcelable()传递了Item及其在Activity Intent中的数据库数据.

A click on the DetailsActivity CardView launches EditActivity to edit that CardView's data. Clicking the "Save" button returns the user to the DetailsActivity so the user can see any edited changes to that CardView. The model for the CardView Item implements Parcelable so I use getParcelable() to pass the item and it's database data in the Activity Intents.

我的问题是,单击保存"后,用户将返回到DetailsActivity CardView,并显示CardView的旧视图(用户在更新数据之前的视图只是输入并保存到数据库中).我尝试使用"adapter.notifyDataSetChanged()"更新视图,但没有运气.

My problem is that after clicking "Save" the user returns to the DetailsActivity CardView and the old View for the CardView is shown (the View prior to the updated data the user just entered and saved to the database). I tried to use "adapter.notifyDataSetChanged()" to update the View, but no luck.

以某种方式,从EditActivity中,我需要重新加载MainActivity,以便RecyclerView更新适配器,然后将用户返回到更新的DetailsActivity CardView.我可以以某种方式使用startActivityForResult()吗?我在这里想念什么?

Somehow from the EditActivity, I need to re-load the MainActivity so the RecyclerView updates the Adapter and then return the user to the updated DetailsActivity CardView. Can I somehow use startActivityForResult()? What am I missing here?

这是UI进度:

MainActivity RecyclerView和适配器:

MainActivity RecyclerView and Adapter:

-适配器使用recyclerItemClickListener将单击的CardView项目发送回MainActivity,因此我可以使用onItemClick()启动DetailsActivity.

--the Adapter uses a recyclerItemClickListener to send the clicked CardView Item back to the MainActivity so I can use onItemClick() to launch DetailsActivity.

-onItemClick()通过Intent启动DetailsActivity

--onItemClick() launches DetailsActivity via an Intent

DetailsActvity:

DetailsActvity:

-使用onClickListener和Intent启动EditActivity

--uses an onClickListener and an Intent to launch EditActivity

EditActivity:

EditActivity:

-我想单击保存"按钮,将新的/更新的CardView数据保存到数据库,然后将用户返回到Details Activity,以便用户可以看到CardView的更改.

--I would like a click on the "Save" button to save the new/updated CardView data to the database and return the user back to the Details Activity so the user can see the changes to the 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();
}

推荐答案

简单地说,在编辑详细信息后,您可以再次在MainActivity的onResume()中读取数据库项.

Simply you can read database items in onResume() in MainActivity again after editing detail.

allList = sqLiteDB.getAllDBItems();
adapter.notifyDatasetChanged();

用简单的话再次从数据库中读取列表并重新绘制它. 并确保将更改保存在 EditActivity

In simple words read list again from database and re draw it. And make sure you saved the change in database in EditActivity

,然后使用startActivityForResult()调用您的 EditActivity ,将其传递回去 setResult();

and call your EditActivity with startActivityForResult() whatever you change pass it back in setResult();

和DetailActivity的 onActivityResult 方法中,使用新值填充数据.

and in onActivityResult method of DetailActivity populate data with new values.

这篇关于RecyclerView:如何刷新Details Activity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆