2017-08-04 10 views
0

私はGlobalClassクラスをグローバルに持っています。グローバルクラスは、オブジェクト内のテキストビューをどのように変更できますか?

したがって、このクラス内で私が何をしたいのか、レイアウト

public class GlobalClass extends Application{ 
    private ClassThatHasTextView foo= null; 

    function(){ 
     foo.text.setText("jdajiodsa"); 
    } 
} 


public class ClassThatHasTextView extends Activity{ 

    TextView text; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d(TAG, "onCreate"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ClassThatHasTextView); 
    } 
} 

が内側にある関数の中で(つまりClassThatHasTextViewの内部にある)textを変更することである含まれている別のクラスのオブジェクトを持っていますGlobalClass、それは可能ですか?

テキストを変更するにはどうすればよいですか?

私は

1) this.foo.text.setText("dsajiojdsa") 
2)this.getApplicationContext().foo.text.setText("idasisda") 

任意のアイデアのようないくつかのsollutions考えていますか?

+0

グローバルにアプリケーションを使用しないでください(シングルトンクラスを使用してください)、私の知っている限り、アプリケーションがアプリケーション内で何もしていないとは限りません。 – Raykud

答えて

-1

あなたが最初にあなたのApplicationクラスでのTextViewを設定していることを確認してくださいあなたのGlobalClass

private TextView textView; 

    public void setText(String text) { 
     if (this.textView != null) 
       this.textView.setText(text) 
    } 

    public void setTextView(TextView textView){ 
     this.textView = textView; 
    } 
あなたのActivityクラスで

 GlobalClass globalClass = (GlobalClass) getApplicationContext(); 
    globalClass.setText("dsajiojdsa"); 

ではセッターとゲッター

を使用することができます。

関連する問題