0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[ Android ] サーバから取得した値をsetterでアクティビティに引き渡す

Posted at

今回はサーバから取得した値をアクティビティに引き渡す方法を記す。

##setter()
セッターというメソッドを作れば簡単に実装できる。
取得値を引き渡したいクラスに、引き渡し用の変数を作り、セッターメソッドでその変数に代入すれば完成である。
具体的には以下のように実装する。

MainActivity.java

public class MainActivity extends Activity {
    private String serverValue = null;

    public void onCreate(Bundle state) {
        //...
        new MyAsync(this).execute();
    }

    public void setList(String serverValue) {
        this.serverValue = serverValue;
    }
}    

MyAsync.java

public class MyAsync extends AsyncTask<Void, Void, List<String>> {

    String serverValue;
    private YourAcitivity activity;

    public MyAsync(MainAcitivity activity) {
        this.activity = activity;
    }

    @Override
    protected List<String> doInBackground(Void... arg0) {
    }

    @Override
    protected void onPostExecute(String serverValue) {
        activity.setList(serverValue);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?