java - 有没有办法将表行保存到 Firebase 中?实时还是 Firestore?

标签 java android firebase

我制作了这个移动应用程序,可以在按下按钮后添加表格行。每行包含一个 Edittext、四个下拉菜单(旋转器)和一个删除行的按钮。当我按下“添加”按钮后,每一行就像一个重复的行。现在,我唯一的问题是如何将该特定内容保存到 firebase 的实时数据库或 firestore 中。你们有关于如何做到这一点的建议、任何资源吗?还是这个问题很广泛?我可以具体说明一些你感到困惑的事情。

我可以保存包含文档标题的编辑文本,并且它可以成功工作,但表行除外,因为它包含多种数据类型。我刚刚听说 firebase 只能保存原始数据类型。

这是与问题相关的代码,因此我不必粘贴所有 357 行代码。

    private android.support.v7.widget.Toolbar maintoolbar, editToolBar;
    private FirebaseAuth mAuth;
    private FloatingActionButton fab;


    private RelativeLayout layoutMain;
    private RelativeLayout layoutButtons;
    private RelativeLayout layoutContent;
    private boolean isOpen = false;


    private Button addnewRowButton, saveItButton;
    private EditText titleofDoc;
    private Context context = null;
    private ProgressBar pb;




    private DatabaseReference dr;

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


        mAuth = FirebaseAuth.getInstance();
        dr = FirebaseDatabase.getInstance().getReference().child("Notes").child(mAuth.getCurrentUser().getUid());

        maintoolbar = findViewById(R.id.mainToolBar);
        setSupportActionBar(maintoolbar);

        getSupportActionBar().setTitle("Files");

        layoutMain = findViewById(R.id.mainLays);
        layoutButtons = findViewById(R.id.layoutButtons);
        layoutContent = findViewById(R.id.layoutContent);


        addnewRowButton = findViewById(R.id.AddRow);
        saveItButton = findViewById(R.id.SaveThis);
        titleofDoc = findViewById(R.id.TitleofDoc);
        editToolBar = findViewById(R.id.main_edit_toolbar);
        pb = findViewById(R.id.progressBar2);


        saveItButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = titleofDoc.getText().toString().trim();

                if (!TextUtils.isEmpty(title)) {
                    pb.setVisibility(View.VISIBLE);
                    createNew(title);
                    pb.setVisibility(View.INVISIBLE);
                } else {
                    Snackbar.make(v, "Fill Empty Fields", Snackbar.LENGTH_SHORT).show();
                    pb.setVisibility(View.INVISIBLE);
                }
            }
        });


        addnewRowButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addNewRow();
            }
        });

        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewMenu();
            }
        });

    }

这是创建表格行的方法。

    private void addNewRow() {
        final TableLayout tl = findViewById(R.id.tbllays);
        String[] teamRoles = {"Director", "Marketing", "Team", "All"};
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, teamRoles);

        context = getApplicationContext();
        //adds new row

        final TableRow tbr = new TableRow(context);

        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        tbr.setLayoutParams(layoutParams);

        //add edittext to name the task
        EditText editText = new EditText(context);
        editText.setHint("Add Task");
        tbr.addView(editText, 0);

        //add spinner to assign teams
        Spinner toDo = new Spinner(context);
        toDo.setAdapter(adapter);
        tbr.addView(toDo, 1);

        //add spinner to assign teams
        Spinner InProgress = new Spinner(context);
        InProgress.setAdapter(adapter);
        tbr.addView(InProgress, 2);

        //add spinner to assign teams
        Spinner Test = new Spinner(context);
        Test.setAdapter(adapter);
        tbr.addView(Test, 3);

        //add spinner to assign teams
        Spinner Done = new Spinner(context);
        Done.setAdapter(adapter);
        tbr.addView(Done, 4);


        // Add a button in the second column
        Button button = new Button(context);
        button.setText("X");
        tbr.addView(button, 5);
        button.setBackgroundColor(Color.rgb(159, 168, 218));

        // Get delete table row button.
        Button deleteRowButton = (Button) button;
        deleteRowButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tbr.removeAllViews();
            }
        });


        tl.addView(tbr);

    }

这种方法只能保存标题,而不能保存内容(本例中为表格行)。

    private void createNew(String title) {
        if (mAuth.getCurrentUser() != null) {
            final DatabaseReference newThingRef = dr.push();

            final Map thingMap = new HashMap();
            thingMap.put("title", title);
            thingMap.put("timestamp", ServerValue.TIMESTAMP);

            Thread mainThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    newThingRef.setValue(thingMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {


                            if (task.isSuccessful()) {
                                Toast.makeText(MainActivity.this, "Note added", Toast.LENGTH_SHORT).show();
                                Intent MainIntent = new Intent(MainActivity.this, MainActivity.class);
                                startActivity(MainIntent);
                            } else {
                                String error = task.getException().getMessage();
                                Toast.makeText(MainActivity.this, "ERROR: " + error, Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                }
            });
            mainThread.start();


        } else {...}

    }

这是xml文件(保存编辑器的部分)

    <RelativeLayout
        android:id="@+id/layoutContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/mainToolBar"
        >


        <RelativeLayout
            android:id="@+id/layoutButtons"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:gravity="center"

            android:visibility="gone">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                tools:context=".MainEditor">

                <LinearLayout
                    android:id="@+id/LinLay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    tools:layout_editor_absoluteX="0dp"
                    tools:layout_editor_absoluteY="370dp">


                    <android.support.v7.widget.Toolbar
                        android:id="@+id/main_edit_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="?attr/colorPrimary"
                        android:minHeight="?attr/actionBarSize"
                        android:theme="?attr/actionBarTheme" />

                    <ProgressBar
                        android:id="@+id/progressBar2"
                        style="?android:attr/progressBarStyleHorizontal"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:indeterminate="true" />

                    <EditText
                        android:id="@+id/TitleofDoc"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:ellipsize="end"
                        android:hint="@string/title"
                        android:importantForAutofill="no"
                        android:inputType=""
                        android:maxHeight="1dp"
                        android:maxLength="30"
                        android:maxLines="1"
                        android:padding="10dp"
                        tools:targetApi="o" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginBottom="10dp"
                        android:alpha=".3"
                        android:background="@android:color/black" />

                    <HorizontalScrollView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fillViewport="true">

                        <TableLayout
                            android:id="@+id/tbllays"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent">

                            <TableRow
                                android:layout_width="match_parent"
                                android:layout_height="match_parent">

                                <Button
                                    android:id="@+id/AddRow"
                                    style="?android:attr/buttonBarButtonStyle"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="0"
                                    android:layout_weight=".1"

                                    android:background="@color/colorAccent"
                                    android:text="@string/add_row"
                                    android:textColor="@color/colorPrimaryDark" />


                                <Button
                                    android:id="@+id/SaveThis"
                                    style="?android:attr/buttonBarButtonStyle"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="1"
                                    android:layout_weight=".1"

                                    android:background="@color/colorPrimaryDark"
                                    android:text="@string/save" />
                            </TableRow>

                            <TableRow
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:stretchColumns="*">


                                <TextView

                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="0"

                                    android:text="@string/task" />

                                <TextView

                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="1"

                                    android:text="@string/to_do" />

                                <TextView

                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="2"
                                    android:layout_weight=".1"
                                    android:text="@string/in_progress" />

                                <TextView

                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="3"
                                    android:layout_weight=".1"
                                    android:text="@string/testing" />

                                <TextView

                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="4"
                                    android:layout_weight=".1"
                                    android:text="@string/done" />

                                <TextView

                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_column="5"
                                    android:layout_weight=".1"
                                    android:text="@string/delete" />


                            </TableRow>

                        </TableLayout>
                    </HorizontalScrollView>

                </LinearLayout>


            </ScrollView>

        </RelativeLayout>

    </RelativeLayout>

忽略用户部分,仅当用户登录时。

This is how it runs

最佳答案

您需要在每个微调器的末尾添加一个监听器并将其存储在变量中,例如:

Spinner Done = new Spinner(context);
Done.setAdapter(adapter);
Done.setOnItemSelectedListner(this)
tbr.addView(Done, 4);

然后使用 DatabaseReference 存储此变量 Done

关于java - 有没有办法将表行保存到 Firebase 中?实时还是 Firestore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56070370/

相关文章:

android - Firebase RemoteConfig 如何为不同应用风格添加键值

java - Docker+Log4j最佳实践

java - Jackson JSON 在序列化之前修改对象

java - 使用 Apache commons 文件上传设置要上传的默认文件

android - 喷气背包导航 : NavHostManager is not an active fragment of FragmentManager

firebase - 克隆 firebase 配置时权限被拒绝

java - 运行时错误 - 跳过一行用户输入(扫描仪)

android - 使用 GCM 推送通知和相应的聊天 Activity

Android KeyEvent.getCharacters() 或 keyEvent.characters 已弃用

java - Android - Firebase 访问级别