第25天~还是Firebase

还是Firebase

SQLite将PostgreSQL作为参考平台。

https://ithelp.ithome.com.tw/upload/images/20220204/20119035cRm7qaaRiV.png

开始改java档

从宣告变数开始~ 原来程序码也要改位置

https://ithelp.ithome.com.tw/upload/images/20220204/201190354zQh1KtpPr.png

package com.huang.myfirebase01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {
    // 宣告变数
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("data");
    EditText editText;



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

        //初始化


    }

    public void onClick(View view) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220204/20119035iWW6afm7nW.png

package com.huang.myfirebase01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {
    // 宣告变数
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("data");
    EditText editText;



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

        //初始化
        editText = findViewById(R.id.editText);
        //确认是否可以连线-第1次测试
        myRef.setValue("Test!");


    }

    public void onClick(View view) {
    }
}


第5步读资料-全部复制近来

https://ithelp.ithome.com.tw/upload/images/20220204/20119035TzeyKjev5f.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035NEtMJvn96t.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035ZEmqFAX5Ke.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035ogKgNF5QdX.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035rG51KBTFAC.png

目前的程序码:

package com.huang.myfirebase01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {
    // 宣告变数
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("data");
    EditText editText;



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

        //初始化
        editText = findViewById(R.id.editText);
        //确认是否可以连线-第1次测试
        myRef.setValue("Test!");

        // Read from the database
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
               
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                
            }
        });




    }

    public void onClick(View view) {
    }
}

先放入Toast来测试看看
https://ithelp.ithome.com.tw/upload/images/20220204/20119035gCwG0QsxoR.png

package com.huang.myfirebase01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {
    // 宣告变数
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("data");
    EditText editText;



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

        //初始化
        editText = findViewById(R.id.editText);
        //确认是否可以连线-第1次测试
        myRef.setValue("Test!");

        // Read from the database
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                Toast.makeText(MainActivity.this,value,Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Toast.makeText(MainActivity.this,"FAIL",Toast.LENGTH_SHORT).show();

            }
        });




    }

    public void onClick(View view) {
    }
}

到目前的程序码:

package com.huang.myfirebase01;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    //宣告变数
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("data");
    EditText editText;

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

        editText = findViewById(R.id.editText);
    }

    public void onClick(View view) {
        switch(view.getId()){
            case R.id.button://新增1
                String str = editText.getText().toString().trim();
                myRef.child("BB").setValue(str);
                //myRef.setValue(str);
                break;
            case R.id.button2://删除
                myRef = database.getReference("data");
                //myRef.removeValue();//移除参考点的所有值
                myRef.child("BB").removeValue();//移除子节点BB的所有值
                break;
            case R.id.button3://更新
                myRef = database.getReference("data");
                HashMap<String, Object> hm = new HashMap<>();
                hm.put("KK", "1111111");
                myRef.updateChildren(hm);
                break;
        }
    }
}

来RUN看看~也是要用手机测试

https://ithelp.ithome.com.tw/upload/images/20220204/20119035sbpmzbSa5a.png

在手机看-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035CFGhtIq4Hm.jpg

https://ithelp.ithome.com.tw/upload/images/20220204/20119035bkYqycFyB3.jpg


新增子节点的方法-

目前长这样-
https://console.firebase.google.com/project/mygjun-f9bb9/database/mygjun-f9bb9-default-rtdb/data

https://ithelp.ithome.com.tw/upload/images/20220204/20119035G9S0g0ytBJ.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035fjHRg0PAb2.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035cQFuQj0QiV.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035Vo7F1MrrkT.png


https://ithelp.ithome.com.tw/upload/images/20220204/20119035DaefOZvSxy.png
来测试这句

case R.id.button2://删除
                myRef = database.getReference("data");
                //myRef.removeValue();//移除参考点的所有值
                myRef.child("BB").removeValue();//移除子节点BB的所有值
                break;

https://ithelp.ithome.com.tw/upload/images/20220204/20119035nEeTWjXNIR.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035BMOhggFd8q.jpg


来测试这句

case R.id.button3://更新
                myRef = database.getReference("data");
                HashMap<String, Object> hm = new HashMap<>();
                hm.put("KK", "1111111");
                myRef.updateChildren(hm);
                break;

按更新只有 hm.put("KK", "1111111");

https://ithelp.ithome.com.tw/upload/images/20220204/20119035OhhO3o7up9.png


再贴一次程序码:

package com.huang.myfirebase01;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    //宣告变数
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("data");
    EditText editText;

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

        editText = findViewById(R.id.editText);
    }

    public void onClick(View view) {
        switch(view.getId()){
            case R.id.button://新增1
                String str = editText.getText().toString().trim();
                myRef.child("BB").setValue(str);
                //myRef.setValue(str);
                break;
            case R.id.button2://删除
                myRef = database.getReference("data");
                //myRef.removeValue();//移除参考点的所有值
                myRef.child("BB").removeValue();//移除子节点BB的所有值
                break;
            case R.id.button3://更新
                myRef = database.getReference("data");
                HashMap<String, Object> hm = new HashMap<>();
                hm.put("KK", "1111111");
                myRef.updateChildren(hm);
                break;
        }
    }
}

最近都在看C#来说说Android和Microsoft Visuall Studio软件操作(C#)很雷的地方:

  1. https://ithelp.ithome.com.tw/upload/images/20211010/20119035PDioIpkaAf.png

在Android的按钮或放框框要填字的地方四边中一定要有一边要有绑定

2.在Microsoft Visuall Studio软件操作(C#)会有按"按钮2下"去启动"Click事件"但是新手不知道总是会很手痒得去按两次,就变成"Click事件"很多次,然後就做不出来了~还好Android没有这个问题
3.在C#里面若要使用关键字前面加上@,在Android里面也是有,不过是真的当"关键字使用"..在JAVA的世界里尤其是网页常用的"springboot"使用的超严重的
4.C#在宣告一个常数时会用const来宣告语法像是一年有12个月就是private const int months=12;但是在Android里面没有
5.C#在命名变数的部分int score;是代表把score命名为整数.然後score=90代表的是score的值.目前的写法都是int score=90比较快

而在Android则是命名按钮RadioGroup sex;
CheckBox c1,c2,c3;
TextView show;

6.然後我觉得x++和x--还有--x和++x也是很大的"迷"
/images/emoticon/emoticon06.gif

这个一定要来个范例的阿~不然怎麽会懂0.0

EX1.
int x=20
int y;
y=x--;
//结果y=20 ; x=19 因为y=i再做x--


EX2.
int x=20
int y;
y=--x;
//结果y=19 ; x=19 因为先做x--再做y=x


EX3.

int x,y,z;
x=10;
y=x++*3;
//结果x先乘以3并指定给y之後,x再执行+1,故y=30;x=11

z=++y*2 //结果y先+1.再乘以2指定给z.故z=62;y=31

希望...可以让我继续有铁人发文的机会~拜托拜托


<<:  [Day 25] Edge Impulse + BLE Sense实现手势动作辨识(下)

>>:  Day28 传播链程序实作

[Day 27] Bevy 游戏引擎 (Part 1)

昨天大概讲完 Rocket 的运用了 所以接下来就来介绍其他东西吧 这次要讲的是 Game Engi...

[Day11] SSTI (Server Side Template Injection)

前言 系列完成3分之1了,今天来谈谈SSTI吧 正文 简介 SSTI,全称Server Side T...

.Net Core Web Api_笔记16_api结合ADO.NET资料库操作part4_资料编辑提交更新

编辑则是会By特定编号捞取指定资料去做编辑 所以需要先把资料回填到表单 修改Show.html 这里...

[区块链&DAPP介绍 Day11] Solidity 教学 - units and globally available variables-2

今日来延续昨日没有写完的全域变数。 Error Handlin //里面可以做一些条件判断,如果为f...

Day16 - 用简单的字串替换实作价值上亿的机器人

线上 Ruby 编辑器:https://runrb.io/ Ruby String 文件:http...