有没有一种方法可以将表行保存到Firebase中?实时还是Firestore?

时间:2019-05-10 03:39:24

标签: 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

2 个答案:

答案 0 :(得分:0)

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

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

然后使用DatabaseReference存储此变量“完成”

答案 1 :(得分:0)

您实际上并不想保存行,而是要保存基础内容

与其将屏幕上的行视为内容本身,不如将它们视为仅显示基础内容。可以根据需要备份,修改这些基础内容,同时可以随时完全重建其在屏幕上的表示形式。

要转换您的代码,建议采取以下一系列步骤:

  1. 创建类似Task类的东西,该类具有您想要的所有字段(例如nameteam)。
  2. Task的列表存储在当前的Activity / Fragment类中,例如private List<Task> myTasks
  3. 编写一种方法displayTasks,该方法将从布局中删除所有现有行,然后遍历myTasks并将它们全部绘制到屏幕上。
  4. 更改addNewRow,以便将新的Task添加到myTasks,然后调用displayTasks
  5. 您现在有了一个任务列表,可以be written directly into Cloud Firestore,以后再取回。

我完全知道,这个答案不是您所追求的简单的“ just do x”,而是您真正需要的答案!所有步骤都非常简单,当然可以根据您的用例进行修改。

一旦您对“屏幕上的内容不是我的数据,只是它的一种表示”的概念感到满意,您可能需要研究LiveDataViewModel。它们比较棘手,但是有很多好处(在链接中有解释)。