首先,我想对我的英语错误表示抱歉。我有一个带有回收站视图的片段,其中我有用于创建广告的表单字段。在Recycler视图中,我使用字段(标题,价格,描述,类型等)制作持有者(标题)。持有物品(照片),持有人(页脚)带按钮"创建广告"。
public class AddRemoveNumberedAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> implements OnPhotoClickListener, OnDeleteButtonClickListener, View.OnClickListener, OnActivatePhoneListener, OnLoadPhoneNumbersListener, OnAllDataCollected {
private static final int ITEM_VIEW_TYPE_HEADER = 0;
private static final int ITEM_VIEW_TYPE_ITEM = 1;
private static final int ITEM_VIEW_TYPE_ADD = 2;
private static final int ITEM_VIEW_TYPE_FOOTER = 3;
private ArrayList<String> photoUrls;
private ArrayList<City> cities;
private PhoneNumbers phoneNumbers;
private NewPhotoViewHolder holder;
private CreateDeclarationFormHolder holderForm ;
private boolean isEdit;
private OnPhotoItemClickListener photoItemListener;
private OnActivatePhoneListener activateListener;
private OnLoadPhoneNumbersListener loadPhoneNumbersListener;
private SubcategoryBody subcategoryBody;
private OnDelarationReadyForSendListener declarationReadyListener;
public boolean isHeader(int position){
return position == 0;
}
public boolean isFooter(int position){
return position == getItemCount()-1;
}
private boolean isAddButton(int position){return position == getItemCount() - 1 - 1/*footer*/;}
//region Constructor
public AddRemoveNumberedAdapter(SubcategoryBody subcategoryBody) {
photoUrls = new ArrayList<>();
cities = new ArrayList<>();
this.subcategoryBody = subcategoryBody;
}
//endregion
//region Adapter methods
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType){
default:
case ITEM_VIEW_TYPE_HEADER:
holderForm = new CreateDeclarationFormHolder(inflater.inflate(R.layout.item_declaration_form_header,parent,false));
holderForm.setSubcategory(subcategoryBody);
return holderForm;
case ITEM_VIEW_TYPE_ADD:
case ITEM_VIEW_TYPE_ITEM:
LayoutInflater inflaterItem = LayoutInflater.from(parent.getContext());
View itemView = inflaterItem.inflate(R.layout.item_photo_list,parent,false);
holder = new NewPhotoViewHolder(itemView);
return holder;
case ITEM_VIEW_TYPE_FOOTER:
return new SendDeclarationHolder(inflater.inflate(R.layout.item_send_declaration_footer,parent,false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case ITEM_VIEW_TYPE_HEADER:
((CreateDeclarationFormHolder)holder).setCitiesList(cities);
((CreateDeclarationFormHolder)holder).setActivateListener(this);
((CreateDeclarationFormHolder)holder).setSpinnerPhoneNumbersList(phoneNumbers);
((CreateDeclarationFormHolder)holder).setLoadNumbersListener(this);
((CreateDeclarationFormHolder)holder).setLayoutVisibility(visibility,smsCount);
break;
case ITEM_VIEW_TYPE_FOOTER:
((SendDeclarationHolder)holder).sendDeclaration.setOnClickListener(this);
break;
case ITEM_VIEW_TYPE_ITEM:
((NewPhotoViewHolder)holder).setOnDeleteButtonListener(this);
Log.e("BIND HOLDER","PHOTO_URL "+ getPhotoUrl(position));
((NewPhotoViewHolder)holder).initWithPhotoPath(getPhotoUrl(position),isEdit);
break;
case ITEM_VIEW_TYPE_ADD:
((NewPhotoViewHolder)holder).setOnPhotoClickListener(this);
((NewPhotoViewHolder)holder).initWithPhotoPath(null,isEdit);
break;
}
}
@Override
public int getItemViewType(int position) {
if(isHeader(position))return ITEM_VIEW_TYPE_HEADER;
else if(isFooter(position))return ITEM_VIEW_TYPE_FOOTER;
else if(isAddButton(position))return ITEM_VIEW_TYPE_ADD;
else return ITEM_VIEW_TYPE_ITEM;
}
@Override
public int getItemCount() {
if(photoUrls!=null)
return 1 /*header*/+ photoUrls.size() + 1/*add_item*/ + 1/*footer*/;
else return 1 /*header*/+ 0/*photos*/ + 1/*add_item*/ + 1/*footer*/;
}
@Override
public void onClick(View view) {
if(holderForm != null) {
holderForm.setDataCollectedListener(this);
holderForm.createDeclaration();
}
}
当我单击按钮创建(在页脚持有者中)时,我在标题中调用方法createDeclaration()。在这个方法中,我从我的字段(EditTexts,Spinners,CheckBoxes,RadioButtons)收集所有数据,并在创建类FormOfDeclaration(在我的例子中,在我的情况下称为类CreateDeclarationFormHolder)中保存所有字段。我返回此类后到我的片段和片段我向服务器发出拉取请求以创建广告使用Retrofit 2.Method createDeclaration in holder(header)将在下面:
@Override
public void createDeclaration() {
link = etLink.getText().toString();
validData = new ValidateFormData(null,null,link);
String phone = "";
if(phoneNumbersModel != null && phoneNumbersModel.getList()!=null && phoneNumbersModel.getList().size()!=0) {
phone =phoneNumbersModel.getList().get(spinnerPhoneNumbers.getSelectedItemPosition() - 1).getPhone();
phoneId = getPhoneId(phoneNumbersModel.getList().get(spinnerPhoneNumbers.getSelectedItemPosition() - 1).getPhone());
}else phoneId = "";
int isdeliv = cbIsDelivery.isChecked() ? 1 : 0;
int isfear = cbIsFear.isChecked() ? 1 : 0;
int suggest_Price = cbSuggestPrice.isChecked() ? 1 : 0;
String uniqId = getUniqId();
String city = getSelectedCity();
int typeOfDeclaration = spinnerType.getSelectedItemPosition();
String title = etTitleOfDeclaration.getText().toString();
String description = etDescription.getText().toString();
String skype = etSkype.getText().toString();
String price = etPrice.getText().toString();
Log.i("CREATE","city: "+city+ " title: " + title + " description: " + description);
//собираем данные из всех полей для создания объекта бланка на отправку объявления на сервер в зависимости от категории в которой находится пользователь
CheckBox checkBox;
int size;
String dataId ="";
ArrayList<String> data = null;
String spinnersIdModelORTypeRadioButton = "";
String spinnerModelORTypeRadioButton = "";
String spinnersIdTypeORManufacture = "";
String spinnerTypeORManufacture = "";
if(subcategoryBody.getParams()!= null && subcategoryBody.getParams().size()!= 0){
for(int i = 0; i< subcategoryBody.getParams().size();i++){
switch (subcategoryBody.getParams().get(i).getSettings().getT()){
case CHECK_BOX :
Log.i("CREATE DECLARATION","CHECK BOX ID:"+ String.valueOf(subcategoryBody.getParams().get(i).getSettings().getId()));
size = subcategoryBody.getParams().get(i).getValues().size();
dataId = String.valueOf(subcategoryBody.getParams().get(i).getSettings().getId());
data = new ArrayList<>();
data.clear();
for(int j = 0 ;j < size;j++){
checkBox = (CheckBox)layout.findViewById(subcategoryBody.getParams().get(i).getValues().get(j).getId());
if(checkBox.isChecked())
data.add(String.valueOf(subcategoryBody.getParams().get(i).getValues().get(j).getId()));
}
break;
case SPINNER:
Log.i("CREATE DECLARATION","SPINNER");
spinnersIdTypeORManufacture = String.valueOf(subcategoryBody.getParams().get(i).getSettings().getId());
Log.i("CREATE DECLARATION","SPINNER + ID: "+ String.valueOf(subcategoryBody.getParams().get(i).getSettings().getId()));
int selectedItemPosition = spinner.getSelectedItemPosition();
Log.i("CREATE DECLARATION","SPINNERS params: "+ "params size :"+ String.valueOf(subcategoryBody.getParams().size())+" i= "+String.valueOf(i)
+" values size of i: "+String.valueOf(subcategoryBody.getParams().get(i).getValues().size())+" selected position: "+selectedItemPosition);
spinnerTypeORManufacture = String.valueOf(subcategoryBody.getParams().get(i).getValues().get(selectedItemPosition - 1).getId());
if(subcategoryBody.getParams().get(i).getValues()!= null && subcategoryBody.getParams().get(i).getValues().get(selectedItemPosition - 1)!=null
&& subcategoryBody.getParams().get(i).getValues().get(selectedItemPosition - 1 ).getParameters()!=null &&
subcategoryBody.getParams().get(i).getValues().get(selectedItemPosition - 1 ).getParameters().size() != 0) {
int selectedItemPositionOfModel = spinnerModel.getSelectedItemPosition();
int sizeExtraParameters = subcategoryBody.getParams().get(i).getValues().get(selectedItemPosition-1).getParameters().size();
for(int j=0; j< sizeExtraParameters;j++) {
spinnersIdModelORTypeRadioButton = String.valueOf(subcategoryBody.getParams().get(i).getValues().get(selectedItemPosition - 1).getParameters().get(0).getSettings().getId());
spinnerModelORTypeRadioButton = String.valueOf(subcategoryBody.getParams().get(i).getValues().get(selectedItemPosition - 1).
getParameters().get(j).getValues().get(selectedItemPositionOfModel-1).getId());
}
}
break;
case RADIO_BUTTON:
spinnersIdModelORTypeRadioButton = String.valueOf(subcategoryBody.getParams().get(i).getSettings().getId());
RadioButton rButton;
for (int j = 0;j < subcategoryBody.getParams().get(i).getValues().size();j++) {
rButton = (RadioButton) layout.findViewById(subcategoryBody.getParams().get(i).getValues().get(j).getId());
if(rButton.isChecked())
spinnerModelORTypeRadioButton = String.valueOf(subcategoryBody.getParams().get(i).getValues().get(j).getId());
}
break;
}
}
DeclarationFormForSending formAntennaUKV = new DeclarationFormForSending(city,typeOfDeclaration,title,
description,link,phoneId,phone,skype,price,isdeliv,isfear,suggest_Price,uniqId,
subcategoryBody.getId(),spinnersIdTypeORManufacture,spinnerTypeORManufacture,spinnersIdModelORTypeRadioButton,spinnerModelORTypeRadioButton,dataId,data);
Log.i("FORM in ANTENNA_UKV",formAntennaUKV.toString());
dataCollectedListener.allDataSending(formAntennaUKV);
}
}
当我第一次制作广告时,我的问题代码没有问题,但是如果我转到另一个片段或活动后,例如它可以是我的菜单或其他应用程序的部分,之后我将再次回到片段&#34;创建广告&#34;我试图用字段中的新数据创建新广告,我的应用程序工作不错,它试图创建这个广告,但我的第二次所有字段都保持空白!!!!!!!为什么会这样? =(