我从Firebase
数据库中获取了价值并将其显示在Textview
上。我想在同一活动的另一个Textview
中显示该值。如何通过?
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_units);
Intent intent = getIntent();
databaseBills = FirebaseDatabase.getInstance().getReference("bills");
Query lastQuery = databaseBills.orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String value = childSnapshot.child("current_units").getValue(String.class);
Log.d("onDataChange", "current_units="+value);
pre_units.setText(value);
Double convert = Double.parseDouble(pre_units.getText().toString());
Intent intent = new Intent(getApplicationContext(), ShowUnits.class);
intent.putExtra("PreValue", convert);
} }
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
textViewCheck = (TextView) findViewById(R.id.textViewCheck);
cur_units = (TextView) findViewById(R.id.cur_units);
pre_units = (TextView) findViewById(R.id.pre_units);
textView17 = (TextView) findViewById(R.id.textView17);
Intent i = getIntent();
final double con = i.getDoubleExtra("Value", 0.0);
final double pre = i.getDoubleExtra("PreValue", 0.0);
cur_units.setText("" + con);
textViewCheck.setText("" +pre);
答案 0 :(得分:0)
使用以下代码:我刚刚添加了两行代码,并删除了其中的一行。检查一下,让我知道,它可以解决
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_units);
Intent intent = getIntent();
//declare textView as final or globally
final TextView textViewCheck = (TextView) findViewById(R.id.textViewCheck);
databaseBills = FirebaseDatabase.getInstance().getReference("bills");
Query lastQuery = databaseBills.orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String value = childSnapshot.child("current_units").getValue(String.class);
Log.d("onDataChange", "current_units="+value);
pre_units.setText(value);
//use this line of code
textViewCheck.setText(String.format("%s", Double.parseDouble(value)));
Double convert = Double.parseDouble(pre_units.getText().toString());
Intent intent = new Intent(getApplicationContext(), ShowUnits.class);
intent.putExtra("PreValue", convert);
} }
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
cur_units = (TextView) findViewById(R.id.cur_units);
pre_units = (TextView) findViewById(R.id.pre_units);
textView17 = (TextView) findViewById(R.id.textView17);
Intent i = getIntent();
final double con = i.getDoubleExtra("Value", 0.0);
final double pre = i.getDoubleExtra("PreValue", 0.0);
cur_units.setText("" + con);
textViewCheck.setText("" +pre);
答案 1 :(得分:0)
textViewCheck
是一个类的字段。您可以在监听器中使用它。
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String value = childSnapshot.child("current_units").getValue(String.class);
Log.d("onDataChange", "current_units="+value);
pre_units.setText(value);
Double convert = Double.parseDouble(value);
textViewCheck.setText(convert.toString());
}
}
您可以为外部侦听器所需的值创建字段。
class MainActivity extends Activity {
// Created a field
private double currentUnits;
@Override
protected void onCreate(Bundle savedInstanceState) {
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String value = childSnapshot.child("current_units").getValue(String.class);
pre_units.setText(value);
// Update value
currentUnits = Double.parseDouble(value);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// It is function outside listener that use currentUnits field
void someFunctionThatUseValueWasRecivedFromListener() {
double squaredValue = currentUnits * cucurrentUnits;
textViewCheck.setText(String.valueOf(squaredValue ));
}
}