将更改保存在片段android

时间:2019-06-22 17:44:20

标签: java android android-fragments android-button android-tablayout

在HomeActivity中,我有一个ButtonNavigationView,其中包含4个带有片段的项目,其中一个(片段)我有一个按钮,可将动态视图添加到线性布局中,我希望当我切换到ButtonNavigationView上的另一个选项卡时它们不会消失/ p>

如您在第一张图片中所见,我通过单击按钮添加了一些动态视图 然后在第二张图片中,我切换到ButtonNavigationView上的另一个选项卡 之后,在第三张图片中,当我返回第二个选项卡时,动态生成的视图都消失了,该怎么保存呢?

HomeActivity

private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    selectedFragment = new FactorsFragment();
                    break;
                case R.id.navigation_dashboard:
                    selectedFragment = new NewFactorFragment();
                    break;
                case R.id.navigation_notifications:
                    selectedFragment = new ProductsFragment();
                    break;
                case R.id.navigation_checklist:
                    selectedFragment = new CheckList();
                    break;
            }

            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();

            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        navigation.setSelectedItemId(R.id.navigation_dashboard);

        SQLiteStudioService.instance().start(this);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new NewFactorFragment());
    }

    @Override
    protected void onDestroy() {
        SQLiteStudioService.instance().stop();
        super.onDestroy();
    }

我需要保存更改的片段

public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_newfactor, container, false);
        ln = v.findViewById(R.id.itemsContainer);
        submit = v.findViewById(R.id.submit);
        editText2 = v.findViewById(R.id.customerName);
        setRetainInstance(true);

        dbConnector = new DbConnector(getContext(), null, null, 1);
        Cursor c = dbConnector.get().rawQuery("SELECT * FROM categories", null);


        while (c.moveToNext()) {

            final int id = c.getInt(c.getColumnIndex("id"));
            String name = c.getString(c.getColumnIndex("name"));

            final View rowView = inflater.inflate(R.layout.field_button, null);
            Button fieldCreator = rowView.findViewById(R.id.fieldCreator);
            fieldContainer = rowView.findViewById(R.id.field_container);
            fieldCreator.setText(name);
            fieldCreator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LinearLayout fieldContainer = ((RelativeLayout) v.getParent()).findViewById(R.id.field_container);
                    fieldContainer.removeAllViews();
                    final Cursor c = dbConnector.get().rawQuery("SELECT * FROM product WHERE category_id = " + id, null);

                    while (c.moveToNext()) {
                        final View rowView = inflater.inflate(R.layout.field, null);

                        TextView productName = rowView.findViewById(R.id.product_name);
                        final EditText editText = rowView.findViewById(R.id.number);
                        Button add = rowView.findViewById(R.id.add);
                        Button remove = rowView.findViewById(R.id.remove);
                        final TextView productPrice = rowView.findViewById(R.id.product_price);

                        productName.setText(c.getString(c.getColumnIndex("name")));
                        final int price_db = Integer.parseInt(c.getString(c.getColumnIndex("price")));


                        editText.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {


                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                if (!editText.getText().toString().equals("")) {
                                    int price = Integer.parseInt(editText.getText().toString()) * price_db;
                                    productPrice.setText(price + "");
                                }
                            }
                        });

                        add.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                int num = Integer.parseInt(editText.getText().toString());
                                int num_num = num + 1;
                                editText.setText(num_num + "");
                            }
                        });

                        remove.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                int num = Integer.parseInt(editText.getText().toString());
                                if (num > 0) {
                                    int num_num = num - 1;
                                    editText.setText(num_num + "");
                                }

                            }
                        });


                        fieldContainer.addView(rowView);

                    }


                }
            });

            ln.addView(rowView);

        }

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < ln.getChildCount(); i++) {
                    RelativeLayout button = (RelativeLayout) ln.getChildAt(i);
                    LinearLayout itemCon = (LinearLayout) button.getChildAt(1);
                    for (int o = 0; o < itemCon.getChildCount(); o++) {
                        JSONObject jsonObject = new JSONObject();
                        LinearLayout field = (LinearLayout) itemCon.getChildAt(o);
                        EditText number = (EditText) field.getChildAt(2);
                        TextView price = (TextView) field.getChildAt(0);
                        TextView name = (TextView) field.getChildAt(3);
                        if (!number.getText().toString().equals("0")) {
                            try {
                                jsonObject.put("product_name", name.getText());
                                jsonObject.put("number", number.getText());
                                jsonObject.put("price", price.getText());
                                jsonObject.put("buyer_name",editText2.getEditText().getText().toString());
                            } catch (JSONException e) {

                            }
                        }
                        jsonArray.put(jsonObject);
                    }

                }

                ContentValues values = new ContentValues();
                values.put("name",editText2.getEditText().getText().toString());
                values.put("items",jsonArray.toString());

                Log.e("JSONobject",jsonArray.toString());

                try {
                    long id = dbConnector.get().insert("factors",null,values);
                }catch (SQLiteException exception){
                    Log.e("ERROR",exception.getMessage());
                }
            }
        });


        return v;
    }

First Image

Second Image

Third image

0 个答案:

没有答案