コメント |
@HTML
<a href="http://winofsql.jp/download/tools.jar">tools.jar
<img src="http://winofsql.jp/image/lightbox_download.jpg" style="border: solid 1px #000000"></a>
@HEND
https://github.com/google/gson(Google Gson)
https://freebase-654b7.firebaseio.com/item.json?print=pretty(JSON データ)
@SHOW
<Map<String, JsonData>>
Firebase の ルートに item を作成して、そのツリーに対して以下のデータをインポートします
https://lightbox.sakura.ne.jp/demo/firebase/jsondata.json
https://lh3.googleusercontent.com/-2YEe7uD5zUY/V_tR28V5l3I/AAAAAAAAhAc/px0DEcH1ELEdntvZIF5V88PDWp3FfIl4gCHM/s700/_img.png
@END
https://lh3.googleusercontent.com/-AaIxzovoJpI/V_soTYky3nI/AAAAAAAAg_8/Z2cBVxSTwVUGBngRJNmTKheYCXE1vT0ywCHM/s400/Screenshot_1476077497.png
[[MainActivity]]
@DIV
public class MainActivity extends AppCompatActivity {
public static int FIRST_PAGE = 0;
public static int NEXT_PAGE = 1;
private ListView listview;
private ViewSwitcher vs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 複数画面処理
vs = (ViewSwitcher) MainActivity.this.findViewById(R.id.viewSwitcher);
// 次画面処理
NextPage nextPage = new NextPage(MainActivity.this);
// イベント登録
nextPage.initAction();
// リストビューの取得
listview = (ListView) MainActivity.this.findViewById(R.id.listView);
// リストビューの行をタップした時の処理
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// アダプターを取得
MyArrayAdapter adapter = (MyArrayAdapter)parent.getAdapter();
// 行データを取得
JsonData json = (JsonData)adapter.getItem(position);
// 次画面に直接データ表示
View include1 = MainActivity.this.findViewById(R.id.include1);
TextView textView = (TextView) include1.findViewById(R.id.textView);
textView.setText(json.getText());
// 画面移動
vs.setDisplayedChild(MainActivity.NEXT_PAGE);
}
});
// JSON データの URL ( Firebase を使用 )
String json_url = "https://freebase-654b7.firebaseio.com/item.json?print=pretty";
// tools.jar の static メソッド
Tools.callHttpGet(json_url, "utf-8", new Tools.OnAsyncTaskListener() {
@Override
public void onAsyncTaskListener(String s) {
// JSON データをデシリアライズ
Gson gson = new Gson();
Type myMap = new TypeToken<Map<String, JsonData>>(){}.getType();
Map<String,JsonData> map = gson.fromJson(s, myMap);
// アダプタを作成して、データをセット
MyArrayAdapter adapter
= new MyArrayAdapter(MainActivity.this,R.layout.list_item);
// map.values() から ArrayList を作成してセット
adapter.addAll(new ArrayList<JsonData>(map.values()));
// リストビューにデータを表示
listview.setAdapter(adapter);
}
});
}
}
@END
[[MyArrayAdapter]]
@DIV
public class MyArrayAdapter extends ArrayAdapter<JsonData> {
// JsonData を格納する、この処理専用の
// ArrayAdapter のカスタマイズ
// コンストラクタで渡された画面の保存
private int mResource;
public MyArrayAdapter(Context context, int resource) {
super(context, resource);
// ArrayAdapter でも、このようにして保存して利用してます
mResource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// 現在の View の取得
// getContext() で super で渡されたものを取得できます
LayoutInflater inflater
= (LayoutInflater) MyArrayAdapter.this.getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
// super で渡されたものは取得できないので自前で用意します
convertView = inflater.inflate(mResource, null);
}
// アダプターより行データを取得
JsonData json = MyArrayAdapter.this.getItem(position);
// 画面にデータをセット
TextView tv;
// Subject
tv = (TextView) convertView.findViewById(R.id.textItem1);
tv.setText(json.getSubject());
// Name
tv = (TextView) convertView.findViewById(R.id.textItem2);
tv.setText(json.getName());
// 行の画面をシステムに返す
return convertView;
}
}
@END |