【问题标题】:I want to pass a data variable to TextView in the same activity我想在同一个活动中将数据变量传递给 TextView
【发布时间】:2019-03-16 06:39:39
【问题描述】:

我从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);

https://i.stack.imgur.com/Fp5x8.png

【问题讨论】:

  • 使value成为一个类变量,这样你也可以在监听器方法之外访问它。
  • 签出this

标签: android firebase variables android-activity textview


【解决方案1】:

使用下面的代码:我刚刚添加了两行代码并删除了一行。看看告诉我,它解决与否

    @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);

【讨论】:

  • 我被检查了,但这段代码并不能解决我的问题。我想通过侦听器之外的方法返回 DataSnapshot 值?
【解决方案2】:

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 ));
    } 
}

【讨论】:

  • 我希望它在听众之外。因为那个值需要做计算函数
  • @hesh 我已经更新了我的答案,以展示如何在侦听器之外使用价值。
  • 但是这段代码不起作用。我认为错误在这里 void someFunctionThatUseValueWasRecivedFromListener {}
  • @hesh 是的,我忘记了括号,抱歉。但是这段代码不应该工作,我隐藏了不重要的代码。我只展示了您需要做的主要更改:创建字段、更新侦听器内的字段值以及侦听器外部字段的使用。
  • 我不知道怎么改。
猜你喜欢
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2012-09-09
  • 2021-02-21
相关资源
最近更新 更多