我是android和Stack Overflow的新手,可以在这里获得帮助。
我使用FragmentA
创建了多个像View Pager
下面的片段。
现在,我有一个要求,当我必须从所有片段中获取所有输入数据并需要将数据提交到某个地方时,单击按钮时就需要单击该按钮。
我只是知道,从FragmentA
滑动到FragmentC
后,我的FragmentA
活动将被销毁,所以我想到了将该活动保存在某个地方,而我刚刚将其保存在List中。
从当前的FragmentA
滑向FragmentC
并回到FragmentA
之后。我试图以自己的情况getTag
/ getId
进入我的EDITText视图,以null
我尝试了多种方法。我是什么
标准中缺少代码,我在代码中做错了什么,
我该如何考虑自己的要求?
我什至尝试使用SharedPreferences
存储最终提交的值,在这种情况下,我遇到了"OutOfMemoryError"
。
然后直接将该视图添加到List
,然后回到片段时,我尝试将该视图添加到父视图中,这对我也不起作用。
谢谢, 沙达卜
FragmentA.java :
public class FragmentA extends Fragment {
//Edit text
TextInputLayout basics_TextInputLayout1, basics_TextInputLayout2,
basics_TextInputLayout3, basics_TextInputLayout4,
basics_TextInputLayout5, basics_TextInputLayout6,
basics_TextInputLayout7, basics_TextInputLayout8,
basics_TextInputLayout9, basics_TextInputLayout10,
basics_TextInputLayout11;
TextInputLayout[] arrayOfEditText;
ArrayList<TextInputLayout> setEditTextLayouts = null;
ArrayList<String> textHintCaptionValuesListFromDb = null;
//ArrayList<View> setTextViewList;
//Add views
ArrayList<View> addEditTextView;
boolean isbuttonClicked=false;
private Bundle savedState = null;
Button button;
boolean onCreate;
VillageServeyBean villageSubmitBasicsFormList;
View v;
//testlistr
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_village_basics_info, container, false);
if(!isbuttonClicked && savedState==null) {
//EditText Boxes
arrayOfEditText = new TextInputLayout[]{basics_TextInputLayout1, basics_TextInputLayout2, basics_TextInputLayout3, basics_TextInputLayout4, basics_TextInputLayout5,
basics_TextInputLayout6, basics_TextInputLayout7, basics_TextInputLayout8, basics_TextInputLayout9, basics_TextInputLayout10, basics_TextInputLayout11};
setEditTextLayouts = new ArrayList<TextInputLayout>();
//getLayout and initialize
DynamicFieldUpdate.getTextInputLayoutBasedOnTagName(arrayOfEditText, v, Constant.BASICS_TAG_EDITTEXT, setEditTextLayouts);
addEditTextView = new ArrayList<View>();
//call weservice for fields
new CallWebservices().execute();
}
if(savedInstanceState != null && savedState == null) {
savedState = savedInstanceState.getBundle("BasicInfoScreen");
}
if(savedState != null) {
setDataToFormOnComingBackAgaing();
}
return v;
}
private void setDataToFormOnComingBackAgaing(/*View v*/) {
if (savedState!= null) {
HashMap<String,ArrayList> listToGetFromBundle = new HashMap<String,ArrayList>();
listToGetFromBundle = (HashMap<String, ArrayList>) savedState.getSerializable("DataInBasicsBundle");
ArrayList<String> listText = (ArrayList) listToGetFromBundle.get("BasicsTextHint");
ArrayList<View> listView = (ArrayList) listToGetFromBundle.get("BasicsInputEditTextValuesView");
for (String text : listText) {
System.out.println("Text Of Basics Screen " + text);
}
TextInputEditText editText0 = (TextInputEditText) listView.get(0);
if (editText0 != null) {
int st = editText0.getId();
TextInputEditText editText= (TextInputEditText) v.findViewById(st);
editText.setText(editText0.getText());
// System.out.println(" Basics id : - " + editText0.getId());
System.out.println(" tagName : - : - " + editText0.getTag());
System.out.println(" hint : - : - " + editText0.getHint());
System.out.println("Values: Basics :- - : - " + editText0.getText());
} TextInputEditText editText1 = (TextInputEditText) listView.get(1);
if (editText1 != null) {
String st = editText1.getTag().toString();
TextInputEditText editText= (TextInputEditText) getActivity().findViewById(st.trim().hashCode());
editText.setText(editText1.getText());
//System.out.println(" Basics id : - " + editText1.getId());
System.out.println(" tagName : - " + editText1.getTag());
System.out.println(" hint : - " + editText1.getHint());
System.out.println("Values: Basics :- - " + editText1.getText());
}
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// setDataToFormOnComingBackAgaing();
// View detailsFrame = getActivity().findViewById(R.id.details);
}
//till here
private boolean getTextFieldsCaptionsValuesFromDb() {
boolean isServicedParsed = false;
String responseData = Utility.callwebService(Constant.GETVILLAGEBASICSSCREENFIELDS, Constant.GET, null);
if (Utility.isNotNullAndEmpty(responseData)) {
System.out.println("Response data := " + responseData);
isServicedParsed = parseResponseData(responseData);
} else {
//show error
}
return isServicedParsed;
}
private boolean parseResponseData(String responseData) {
boolean flag = false;
try {
JSONArray jsonArray = new JSONArray(responseData);
textHintCaptionValuesListFromDb = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
textHintCaptionValuesListFromDb.add(jsonObj.getString("textFields"));
System.out.println("Data ya aaaa : " + jsonObj.getString("textFields"));
if(!flag) {
flag= true;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return flag;
}
private class CallWebservices extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPostExecute(Boolean sucess) {
if (sucess) {
DynamicFieldUpdate.setTextInputLayout(setEditTextLayouts, textHintCaptionValuesListFromDb, getContext(), 1,addEditTextView);
}
}
@Override
protected Boolean doInBackground(Void... voids) {
return getTextFieldsCaptionsValuesFromDb();
}
}
//adding for saving instance of fragments
@Override
public void onDestroyView() {
super.onDestroyView();
savedState = saveState();
}
private Bundle saveState() {
Bundle state = new Bundle();
VillageServeyBean villageServeyBean = new VillageServeyBean();
ArrayList<Map<String,ArrayList>> listToAddInShrdPrefncAndBundl= new ArrayList<Map<String,ArrayList>>();
//GetData and add in bean
GetAddedListToAddInBundle(listToAddInShrdPrefncAndBundl,state);
setFormSubmitDataHere(listToAddInShrdPrefncAndBundl);
return state;
}
private void GetAddedListToAddInBundle(ArrayList<Map<String, ArrayList>> listToAddInBundle, Bundle state ) {
HashMap<String,ArrayList> putListToBundle =new HashMap<String,ArrayList>();
ArrayList<View> test = new ArrayList<>();
if(textHintCaptionValuesListFromDb!=null) {
putListToBundle.put("BasicsTextHint", textHintCaptionValuesListFromDb);
}
if(addEditTextView != null)
putListToBundle.put("BasicsInputEditTextValuesView", addEditTextView);
listToAddInBundle.add(putListToBundle);
state.putSerializable("DataInBasicsBundle",putListToBundle);
EditText editT = (EditText) addEditTextView.get(0);
/*int st = editT.getId();
View editText= (View) getActivity().findViewById(st) ;*/
test.add(editT);
state.putSerializable("sub_conatainer1_Basic",test);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle("BasicInfoScreen", (savedState != null) ? savedState : saveState());
}
public void setFormSubmitDataHere(ArrayList<Map<String,ArrayList>> submitFormList){
villageSubmitBasicsFormList = new VillageServeyBean();
villageSubmitBasicsFormList.setListToAddInShrdPrefnc(submitFormList);
}
public VillageServeyBean getFormSubmitDataHere(){
return villageSubmitBasicsFormList;
}
}
并创建了“以下方法”以实用地创建布局的一些视图,例如EditText和Spinner等
public class DynamicFieldUpdate {
public static void setCheckBoxes(LinearLayout layout, ArrayList<String> list, Context context,ArrayList<View> viewList ) {
for (int i = 0; i < list.size(); i++) {
CheckBox cb = new CheckBox(context);
cb.setText(list.get(i));
cb.setId(list.get(i).hashCode());
layout.addView(cb);
}
viewList.add(layout);
}
public static void setTextView(LinearLayout layout,String setText,Context context,int size,ArrayList<View> viewList ) {
TextView tv = new TextView(context);
tv.setText(setText);
tv.setTag(setText);
tv.setTextSize(size);
layout.addView(tv);
viewList.add(tv);
}
public static void setEditText(TextInputLayout layout,String setHint , Context context,int i,ArrayList<View> viewList ){
TextInputEditText textInputLayout = new TextInputEditText(context);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textInputLayout.setLayoutParams(editTextParams);
System.out.println("Integer values of hint :-"+setHint.trim().hashCode() + " field"+setHint);
textInputLayout.setId(setHint.trim().hashCode());
textInputLayout.setTag(layout.getTag());
textInputLayout.setHint(setHint);
textInputLayout.setInputType(getInputType(i));
layout.addView(textInputLayout);
viewList.add(textInputLayout);
}
public static void setCustomRadioGroup(LinearLayout layout,Context context,ArrayList<String> setName,ArrayList<View> viewList ){
int sizeOfList=setName.size();
final RadioButton[] radioButtons = new RadioButton[setName.size()];
RadioGroup radioGroup = new RadioGroup(context);
//radioGroup.setTag();//create the RadioGroup
if(setName.size()<3){
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
} else {
radioGroup.setOrientation(RadioGroup.VERTICAL);
}
//or RadioGroup.VERTICAL
for(int i=0; i<setName.size(); i++) {
radioButtons[i] = new RadioButton(context);
radioButtons[i].setText(setName.get(i));
radioButtons[i].setId(i);
radioGroup.addView(radioButtons[i]);
}
layout.addView(radioGroup);
viewList.add(radioGroup);
}
private static int getInputType(int i) {
int retValue = InputType.TYPE_NULL;
switch (i) {
case 1:
retValue = InputType.TYPE_CLASS_TEXT;
break;
case 2:
retValue = InputType.TYPE_CLASS_NUMBER;
break;
case 4:
retValue = InputType.TYPE_CLASS_PHONE;
break;
case 5:
retValue = InputType.TYPE_TEXT_VARIATION_PASSWORD;
break;
case 6:
retValue = InputType.TYPE_CLASS_PHONE;
break;
case 7:
retValue = InputType.TYPE_CLASS_DATETIME;
break;
default:
break;
}
return retValue;
}
// get linear layouts from Views on bases of TAG name, and initialize that view
public static void getLinearLayoutBasedOnTagName(LinearLayout[] layout ,View v,String tagName,ArrayList setListOfLinearLayout){
for(int i = 0 ; i<layout.length;i++){
int k = i+1;
layout[i]=(LinearLayout) v.findViewWithTag(tagName+k);
setListOfLinearLayout.add(layout[i]);
}
}
// get EDITText layouts from Views on bases of TAG name, and initialize that view
public static void getTextInputLayoutBasedOnTagName(TextInputLayout[] layout ,View v,String tagName,ArrayList setListOfTextInputLayout){
for(int i = 0 ; i<layout.length;i++){
int k = i+1;
layout[i]=(TextInputLayout) v.findViewWithTag(tagName+k);
setListOfTextInputLayout.add(layout[i]);
}
}
//set radio button values to corresponding caption
public static void setRadioButtonsValues(ArrayList<LinearLayout> setRadioGroupValuesLinearLayoutList,ArrayList<ArrayList<String>> radionButtonTextCaptionValuesListFromDb,Context context,ArrayList<View> viewList ) {
for (int i = 0; i < setRadioGroupValuesLinearLayoutList.size(); i++) {
DynamicFieldUpdate.setCustomRadioGroup(setRadioGroupValuesLinearLayoutList.get(i), context,radionButtonTextCaptionValuesListFromDb.get(i),viewList);
}
}
//set radio button caption text
public static void setTextViewCaptionValues(ArrayList<LinearLayout> setRadioButtonCaptionsLayoutList,ArrayList radionButtonTextCaptionValuesListFromDb,Context context, int size ,ArrayList<View> viewList ) {
for (int i = 0; i < setRadioButtonCaptionsLayoutList.size(); i++) {
DynamicFieldUpdate.setTextView(setRadioButtonCaptionsLayoutList.get(i), radionButtonTextCaptionValuesListFromDb.get(i).toString(), context, size,viewList);
}
}
//set edit text
public static void setTextInputLayout(ArrayList<TextInputLayout> setTextInputLayout,ArrayList radionButtonTextCaptionValuesListFromDb,Context context, int inputType ,ArrayList<View> viewList){
for (int i = 0; i < setTextInputLayout.size(); i++) {
DynamicFieldUpdate.setEditText(setTextInputLayout.get(i), radionButtonTextCaptionValuesListFromDb.get(i).toString(), context, inputType,viewList);
}
}
public static void getMultipleCheckLayoutBasedOnTagName(MultiSelectionSpinner[] layout ,View v,String tagName,ArrayList setListOfMultiSelectionSpinnerLayout){
for(int i = 0 ; i<layout.length;i++){
int k = i+1;
setListOfMultiSelectionSpinnerLayout.add(tagName+k);
}
}
public static void setMultiCheckBoxesLayout(ArrayList<String> tagName,View v,ArrayList<ArrayList<String>> listToAdd, MultiSelectionSpinner.OnMultipleItemsSelectedListener listner,Context context,ArrayList<View> viewList){
for (int i = 0; i < tagName.size(); i++) {
DynamicFieldUpdate.setMultipleCheckBoxesSelector(tagName.get(i),v, listToAdd.get(i),listner, context,viewList);
}
}
public static void setMultipleCheckBoxesSelector(String tagName, View v, ArrayList<String> listToAdd, MultiSelectionSpinner.OnMultipleItemsSelectedListener listner, Context context,ArrayList<View> viewList){
MultiSelectionSpinner multiSelectionSpinner1 = (MultiSelectionSpinner) v.findViewWithTag(tagName);
multiSelectionSpinner1.setItems(listToAdd);
multiSelectionSpinner1.setSelection(new int[]{0});
multiSelectionSpinner1.setListener(listner);
viewList.add(multiSelectionSpinner1);
}
}
}