首页 > 解决方案 > 将 TextView 插入 Firebase

问题描述

如何将 TextView 的文本放入 Firebase 数据库?

“计数”是 TextView。

也许错误是“String dt=txtdistance.getText().toString();”

//Insert Data to Firebase
    txtlocation= findViewById(R.id.search);
    txtdistance=findViewById(R.id.count);
    bt=(Button)findViewById(R.id.button);
    mDatabase=FirebaseDatabase.getInstance();
    ref=mDatabase.getReference().child("location");

    Button bt = (Button) findViewById(R.id.button);

    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String lc=txtlocation.getText().toString();
            String dt=txtdistance.getText().toString();
            DatabaseReference mRef=ref;
            mRef.child("location").setValue(lc);
            mRef.child("distance").setValue(dt);

            startActivity(new Intent(Dashboard.this, Filter.class));
        }
    });

标签: android

解决方案


@George Sepetadelis 这是我所有的Dasboard代码。

public class Dashboard extends AppCompatActivity {

private TextView count;
private SeekBar seek_Bar;

Button bt;
EditText txtlocation, txtdistance;
DatabaseReference ref;
FirebaseDatabase mDatabase;


@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    //seekbar
    count = findViewById(R.id.count);
    seek_Bar = findViewById(R.id.seekBar);
    seek_Bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int i = 0;
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            i = progress;
            count.setText(""+i+" χλμ");
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    //Insert Data to Firebase
    txtlocation= findViewById(R.id.search);
    txtdistance=findViewById(R.id.count);
    bt=(Button)findViewById(R.id.button);
    mDatabase=FirebaseDatabase.getInstance();
    ref=mDatabase.getReference().child("location");

    Button bt = (Button) findViewById(R.id.button);

    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String lc=txtlocation.getText().toString();
            String dt=txtdistance.getText().toString();
            ref.child("location").setValue(lc);
            ref.child("distance").setValue(dt);

            startActivity(new Intent(Dashboard.this, Filter.class));
        }
    });

}

}


推荐阅读