拡張子:txtvbswsfjsphpjavahtmlutf8sjis public class MainActivity extends AppCompatActivity { public static int FIRST_PAGE = 0; public static int NEXT_PAGE = 1; private ListView listview; private ViewSwitcher vs; private FirebaseDatabase database; private DatabaseReference mDatabase; private ActivityMainBinding binding; private NextPage nextPage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); // Firebase Database 用 database = FirebaseDatabase.getInstance(); mDatabase = database.getReference(); // 複数画面処理 vs = (ViewSwitcher) MainActivity.this.findViewById(R.id.viewSwitcher); // 次画面処理 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); binding.setJdata( json ); nextPage.setData( json ); // 画面移動 vs.setDisplayedChild(MainActivity.NEXT_PAGE); } }); // Firebase の item ツリーを name でソートして取得 mDatabase.child("item").orderByChild("name") .addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if ( dataSnapshot.exists() ) { // 空の ArrayList ArrayList<JsonData> listData = new ArrayList<JsonData>(); // name でソートされたデータを dataSnapshot より取得 Iterator<DataSnapshot> child = dataSnapshot.getChildren().iterator(); while(child.hasNext()) { DataSnapshot next = child.next(); // JsonData に変換 JsonData jsonData = next.getValue(JsonData.class); // ArrayList に追加 listData.add(jsonData); } // アダプタを作成して、データをセット MyArrayAdapter adapter = new MyArrayAdapter(MainActivity.this,R.layout.list_item); //listData は name でソートされた ArrayList adapter.addAll(listData); // リストビューにデータを表示 listview.setAdapter(adapter); } else { Log.i("lightbox","データを読み込めませんでした"); } } @Override public void onCancelled(DatabaseError databaseError) { Log.i("lightbox","onCancelled"); } }); } } MyArrayAdapter 拡張子:txtvbswsfjsphpjavahtmlutf8sjis 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) { // この行用の Data Binding 用 ListItemBinding binding; if (convertView == null) { // 現在の View の取得 // getContext() で super で渡されたものを取得できます LayoutInflater inflater = (LayoutInflater) MyArrayAdapter.this.getContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE); // Data Binding 用のインスタンスを作成 binding = DataBindingUtil.inflate(inflater, mResource, parent, false); } else { // Data Binding 用のインスタンスを取得 binding = DataBindingUtil.getBinding(convertView); } // アダプターより行データを取得 JsonData json = MyArrayAdapter.this.getItem(position); binding.setJdata(json); // Data Binding 用のインスタンスより、必要な View( 画面 ) を取得して返す return binding.getRoot(); } }
public class MainActivity extends AppCompatActivity { public static int FIRST_PAGE = 0; public static int NEXT_PAGE = 1; private ListView listview; private ViewSwitcher vs; private FirebaseDatabase database; private DatabaseReference mDatabase; private ActivityMainBinding binding; private NextPage nextPage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); // Firebase Database 用 database = FirebaseDatabase.getInstance(); mDatabase = database.getReference(); // 複数画面処理 vs = (ViewSwitcher) MainActivity.this.findViewById(R.id.viewSwitcher); // 次画面処理 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); binding.setJdata( json ); nextPage.setData( json ); // 画面移動 vs.setDisplayedChild(MainActivity.NEXT_PAGE); } }); // Firebase の item ツリーを name でソートして取得 mDatabase.child("item").orderByChild("name") .addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if ( dataSnapshot.exists() ) { // 空の ArrayList ArrayList<JsonData> listData = new ArrayList<JsonData>(); // name でソートされたデータを dataSnapshot より取得 Iterator<DataSnapshot> child = dataSnapshot.getChildren().iterator(); while(child.hasNext()) { DataSnapshot next = child.next(); // JsonData に変換 JsonData jsonData = next.getValue(JsonData.class); // ArrayList に追加 listData.add(jsonData); } // アダプタを作成して、データをセット MyArrayAdapter adapter = new MyArrayAdapter(MainActivity.this,R.layout.list_item); //listData は name でソートされた ArrayList adapter.addAll(listData); // リストビューにデータを表示 listview.setAdapter(adapter); } else { Log.i("lightbox","データを読み込めませんでした"); } } @Override public void onCancelled(DatabaseError databaseError) { Log.i("lightbox","onCancelled"); } }); } }
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) { // この行用の Data Binding 用 ListItemBinding binding; if (convertView == null) { // 現在の View の取得 // getContext() で super で渡されたものを取得できます LayoutInflater inflater = (LayoutInflater) MyArrayAdapter.this.getContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE); // Data Binding 用のインスタンスを作成 binding = DataBindingUtil.inflate(inflater, mResource, parent, false); } else { // Data Binding 用のインスタンスを取得 binding = DataBindingUtil.getBinding(convertView); } // アダプターより行データを取得 JsonData json = MyArrayAdapter.this.getItem(position); binding.setJdata(json); // Data Binding 用のインスタンスより、必要な View( 画面 ) を取得して返す return binding.getRoot(); } }
拡張子:txtvbswsfjsphpjavahtmlutf8sjis public class NextPage { private MainActivity mainActivity; private ViewSwitcher vs; private JsonData json; public NextPage(MainActivity mainActivity) { this.mainActivity = mainActivity; } public void setData( JsonData json ) { this.json = json; } public void initAction(){ // 複数画面処理 vs = (ViewSwitcher) mainActivity.findViewById(R.id.viewSwitcher); // activity_next.xml の 親 view です View include1 = mainActivity.findViewById(R.id.include1); // ***************************************** // 戻るボタン // ***************************************** Button backButton = (Button)include1.findViewById(R.id.backButton); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 最初の画面へ移動 vs.setDisplayedChild(MainActivity.FIRST_PAGE); // 画面の更新処理 json.setSubject( json.getSubject() ); json.setName( json.getName() ); json.setText( json.getText() ); } }); } }
public class NextPage { private MainActivity mainActivity; private ViewSwitcher vs; private JsonData json; public NextPage(MainActivity mainActivity) { this.mainActivity = mainActivity; } public void setData( JsonData json ) { this.json = json; } public void initAction(){ // 複数画面処理 vs = (ViewSwitcher) mainActivity.findViewById(R.id.viewSwitcher); // activity_next.xml の 親 view です View include1 = mainActivity.findViewById(R.id.include1); // ***************************************** // 戻るボタン // ***************************************** Button backButton = (Button)include1.findViewById(R.id.backButton); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 最初の画面へ移動 vs.setDisplayedChild(MainActivity.FIRST_PAGE); // 画面の更新処理 json.setSubject( json.getSubject() ); json.setName( json.getName() ); json.setText( json.getText() ); } }); } }
拡張子:txtvbswsfjsphpjavahtmlutf8sjis public class JsonData extends BaseObservable { private String text; private String subject; private String name; private String datetime; public JsonData(){} @Bindable public String getSubject() { return subject; } @Bindable public String getName() { return name; } @Bindable public String getText() { return text; } @Bindable public String getDatetime() { return datetime; } public void setDatetime(String datetime) { this.datetime = datetime; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.datetime); } public void setName(String name) { this.name = name; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.name); } public void setSubject(String subject) { this.subject = subject; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.subject); } public void setText(String text) { this.text = text; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.text); } @Override public String toString() { return this.subject; } }
public class JsonData extends BaseObservable { private String text; private String subject; private String name; private String datetime; public JsonData(){} @Bindable public String getSubject() { return subject; } @Bindable public String getName() { return name; } @Bindable public String getText() { return text; } @Bindable public String getDatetime() { return datetime; } public void setDatetime(String datetime) { this.datetime = datetime; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.datetime); } public void setName(String name) { this.name = name; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.name); } public void setSubject(String subject) { this.subject = subject; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.subject); } public void setText(String text) { this.text = text; notifyPropertyChanged(lightbox.july.listviewandnextactivity.BR.text); } @Override public String toString() { return this.subject; } }
拡張子:txtvbswsfjsphpjavahtmlutf8sjis <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto"> <data> <variable name="jdata" type="lightbox.july.listviewandnextactivity.JsonData"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <ViewSwitcher android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewSwitcher"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listView"/> <include android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/activity_next" android:id="@+id/include1" bind:jdata="@{jdata}"/> </ViewSwitcher> </LinearLayout> </layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto"> <data> <variable name="jdata" type="lightbox.july.listviewandnextactivity.JsonData"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <ViewSwitcher android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewSwitcher"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listView"/> <include android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/activity_next" android:id="@+id/include1" bind:jdata="@{jdata}"/> </ViewSwitcher> </LinearLayout> </layout>
拡張子:txtvbswsfjsphpjavahtmlutf8sjis <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="jdata" type="lightbox.july.listviewandnextactivity.JsonData"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_back" android:id="@+id/backButton" android:layout_alignParentTop="true" android:layout_alignParentStart="true"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:text="@{jdata.text}" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView"/> </LinearLayout> </ScrollView> </LinearLayout> </layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="jdata" type="lightbox.july.listviewandnextactivity.JsonData"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_back" android:id="@+id/backButton" android:layout_alignParentTop="true" android:layout_alignParentStart="true"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:text="@{jdata.text}" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView"/> </LinearLayout> </ScrollView> </LinearLayout> </layout>
拡張子:txtvbswsfjsphpjavahtmlutf8sjis <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="jdata" type="lightbox.july.listviewandnextactivity.JsonData"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <TextView android:text="@{jdata.subject}" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textItem1" /> <TextView android:text="@{jdata.name}" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textItem2" android:layout_marginTop="5dp"/> </LinearLayout> </layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="jdata" type="lightbox.july.listviewandnextactivity.JsonData"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <TextView android:text="@{jdata.subject}" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textItem1" /> <TextView android:text="@{jdata.name}" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textItem2" android:layout_marginTop="5dp"/> </LinearLayout> </layout>