ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
Android 後期試験 解答手順概要 (2) / 画面表示
日時: 2016/01/11 13:06
名前: lightbox



画面表示の単純なテキスト部分は、Android SDK の バインドを使用するので、JSON のフォーマットと画面の定義が一致しておれば、表示されます
activity_main.xml
拡張子:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:bind="http://schemas.android.com/apk/res-auto"
        tools:context=".MainActivity">

    <data>
        <variable
            name="user"
            type="sample.lightbox.abdbupdate.Syain"/>
    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="GET"
                android:id="@+id/getData"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:id="@+id/editScode"
                android:background="@drawable/border"
                android:padding="5dp"
                android:layout_marginBottom="20dp"/>

            <include
                layout="@layout/update_contents"
                android:id="@+id/ex"
                bind:user="@{user}"/>

            <include
                layout="@layout/update_contents2"
                android:id="@+id/ex2"
                bind:user="@{user}"/>

            <include
                layout="@layout/update_contents3"
                android:id="@+id/ex3"
                bind:user="@{user}"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="UPDATE"
                android:id="@+id/updateData"/>

        </LinearLayout>
    </ScrollView>
</layout>
メンテナンス

単純テキスト部分(1) / update_contents.xml ( No.1 )
日時: 2016/01/11 13:09
名前: lightbox


日時: 2016/01/11 13:09
名前: lightbox
この部分はサンプルとして既に表示されるようになっています。
拡張子:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity">

    <data>
        <variable
            name="user"
            type="sample.lightbox.abdbupdate.Syain"/>
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.kj}"
            android:id="@+id/editKj"
            android:background="@drawable/border"
            android:padding="5dp"
            android:layout_marginBottom="2dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.furi}"
            android:id="@+id/editFuri"
            android:background="@drawable/border"
            android:padding="5dp"/>

    </LinearLayout>
</layout>
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
画面にデータが表示される処理の流れとメカニズム ( No.2 )
日時: 2016/01/11 13:19
名前: lightbox
1) 社員コードを入力する
2) ボタンをクリックする
3) ButtonAction クラス
4) 『読み出し』処理で、インターネットより JSON を読み込む
5) Google Gson で オブジェクトに変換
6) バインド SDK によって自動作成されたクラスにそのオブジェクトを引き渡す
7) バインド SDK によって、オブジェクト内の変数に対応する id を持つ画面に表示される
Syain クラス
拡張子:
package sample.lightbox.abdbupdate;

import android.databinding.BaseObservable;

public class Syain extends BaseObservable {

	// シリアライズ・デシリアライズをしない( static )
	static public MainActivity context = null;

	public String type;
	public String status;

	public String scode;
	public String kj;
	public String furi;
	public String kyuyo;
	public String teate;

	public Syain() {
		this.type = "";
		this.status = "";

		this.scode = "";
		this.kj = "";
		this.furi = "";
	}

}
コンストラクタで設定している値は、初期画面で表示させたい値です
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
ButtonAction クラス ( No.3 )
日時: 2016/01/11 13:21
名前: lightbox
拡張子:
package sample.lightbox.abdbupdate;

import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import sample.lightbox.abdbupdate.databinding.ActivityMainBinding;


// *************************************************
// ボタンイベント処理用クラス
// *************************************************
public class ButtonAction implements View.OnClickListener {

	// エミュレータ用
	private String webPage1 = "http://10.0.2.2/atest/dbdata_get_json.php";
	private String webPage2 = "http://10.0.2.2/atest/dbdata_update_json.php";
	// WiFi 実機用( 192.168.1.2 は 自分の PC の IP アドレスに書き換えます )
//	private String webPage1 = "http://192.168.1.2/atest/dbdata_get_json.php";
//	private String webPage2 = "http://192.168.1.2/atest/dbdata_update_json.php";

	// 固定データ
	private MainActivity context = null;
	// 処理毎に変化するデータ
	private Syain syain = null;
	private ActivityMainBinding binding = null;

	// コンストラクタ( 固定データのセット )
	public ButtonAction( MainActivity context) {
		this.context = context;
	}

	// 画面バインド用データ( 処理毎に変化するデータのセット )
	public void setData(ActivityMainBinding binding) {
		this.binding = binding;
	}

	@Override
	public void onClick(View v) {

		// *************************************************
		// 読み出し
		// *************************************************
		if (v.getId() == R.id.getData) {

			String scode = ((EditText) context.findViewById(R.id.editScode)).getText().toString();
			Map map = new HashMap();
			map.put("scode", scode);

			Tools1114.callHttpGet(
					webPage1,
					"utf-8",
					map,
					new Tools1114.OnAsyncTaskListener() {
						@Override
						public void onAsyncTaskListener(String s) {

							Log.i("lightbox", s);

							Gson gson = new Gson();
							syain = gson.fromJson(s,Syain.class);
							binding.setUser(syain);

							Tools1114.setFocusNoKeyboard(context, R.id.editKj);


						}
					}
			);

		}

		// *************************************************
		// 更新
		// *************************************************
		if (v.getId() == R.id.updateData) {

			// 更新用転送
			syain = new Syain();
			syain.scode = ((EditText)context.findViewById(R.id.editScode)).getText().toString();
			syain.kj = ((EditText)context.findViewById(R.id.editKj)).getText().toString();
			syain.furi = ((EditText)context.findViewById(R.id.editFuri)).getText().toString();

			Gson gson = new Gson();
			String json = gson.toJson(syain);

			Map map = new HashMap();
			map.put("sdata", json);

			Tools1114.callHttpGet(
					webPage2,
					"utf-8",
					map,
					new Tools1114.OnAsyncTaskListener() {
						@Override
						public void onAsyncTaskListener(String s) {

							Log.i("lightbox", s);

							Gson gson = new Gson();
							syain = gson.fromJson(s,Syain.class);

							if ( !syain.status.equals( "ERROR" ) ) {
								syain.kj = "";
								syain.furi = "";
							}
							binding.setUser(syain);

							// フォーカスを社員コードへ
							Tools1114.setFocusNoKeyboard(context, R.id.editScode);

						}
					}
			);

		}

	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス