我目前正在我的应用程序中建立一个活动,使用FAB添加产品。单击FAB会打开一个带有startActivityForResult的新弹出式活动,并在其中填充表格并通过Parcelable对象将其发送回去。
我现在面临的问题是,添加产品后,它没有显示在RecyclerView中,但是当我再次单击FAB时,第二个我开始编辑表单中的任何字段(例如,任何EditText) )针对下一个产品,我会在弹出式表格的背景中看到上一个产品。当我完成第二个产品时,直到我开始编辑第三个产品时,它才会出现,等等。
我遍历了XML,对我来说似乎还行。。。尝试读取几个解决方案(以及一次又一次地阅读我的代码),但是没有一个真正有用。
相关代码段(我在...中放了一些与本例无关的代码): 从主要活动(其中应显示产品):
'''
public class EditOffersActivity extends AppCompatActivity {
...
private ArrayList<Offer> offers_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_products);
offers_list = new ArrayList<>();
...
RecyclerView rvOffers = findViewById(R.id.offers_RecyclerView);
OfferAdapter adapter = new OfferAdapter(offers_list);
rvOffers.setAdapter(adapter);
rvOffers.setLayoutManager( new LinearLayoutManager(this));
// Setting up the FAB so it leads to the Product Details Pop-up
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(EditOffersActivity.this, ProductDetailsPopupActivity.class);
startActivityForResult(intent, HTZ_ADD_OFFER); // HTZ_ADD_OFFER = 1
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Offer created_offer = data.getParcelableExtra("offer");
offers_list.add(created_offer);
}
}
'''
适配器: '''
public class OfferAdapter extends android.support.v7.widget.RecyclerView.Adapter<OfferAdapter.OfferViewholder> {
private ArrayList<Offer> mOffers;
public OfferAdapter(ArrayList<Offer> offers) {
mOffers = offers;
}
@Override
public OfferViewholder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.item_product;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layoutIdForListItem, viewGroup, false);
OfferViewholder viewHolder = new OfferViewholder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(OfferViewholder holder, int position) {
Offer current_offer = mOffers.get(position);
... // a lot of binding code
}
@Override
public int getItemCount() {
return mOffers.size();
}
class OfferViewholder extends RecyclerView.ViewHolder {
TextView name_TextView;
TextView time_TextView;
...
public OfferViewholder(View itemView) {
super(itemView);
// Init all views
name_TextView = itemView.findViewById(R.id.product_item_name_TextView);
time_TextView = itemView.findViewById(R.id.product_item_time_TextView);
...
}
}
}
'''
在用户单击“完成”后,弹出活动中的将其关闭的位: '''
mPublishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(p_photo_url == null) {
...
} else {
// Create offer from user input
Offer n_offer = new Offer (...)
);
...
// Return offer to EditOffersActivity
Intent resultIntent = new Intent();
resultIntent.putExtra("offer", n_offer); // Offer is Parcelable
setResult(HTZ_ADD_OFFER, resultIntent);
finish();
}
}
});
'''
还有RecyclerView的XML:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/offers_RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/item_product"
tools:orientation="vertical"
tools:scrollbars="horizontal"
tools:spanCount="1">
答案 0 :(得分:2)
您可以使用notifydatasetchanged如下代码:
mPublishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(p_photo_url == null) {
...
} else {
// Create offer from user input
Offer n_offer = new Offer (...)
);
...
// Return offer to EditOffersActivity
Intent resultIntent = new Intent();
resultIntent.putExtra("offer", n_offer); // Offer is Parcelable
setResult(HTZ_ADD_OFFER, resultIntent);
OfferAdapter.notifydatasetchanged();
finish();
}
}
});
OfferAdapter.notifydatasetchanged(); //方法将刷新您在视图中设置的适配器。值将刷新。
别忘了欣赏和喜欢答案。
答案 1 :(得分:1)
在适配器上添加如下方法:
public void update(ArrayList<Offer> offers){
mOffers = offers;
}
更新您的onActivityResult并像这样通知适配器:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Offer created_offer = data.getParcelableExtra("offer");
offers_list.add(created_offer);
adpater.update(offers_list);
adapter.notifydatasetchanged();
}
答案 2 :(得分:0)
添加到ArrayList后,您必须通过调用RecyclerViewAdapter的 notifydatasetchanged()
来通知对适配器的更改喜欢:
adapter.notifydatasetchanged()