質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

1回答

1271閲覧

タブの情報更新はフラグメントクラスのどのイベントでかくのでしょうか?

SmartBuzz

総合スコア81

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2016/10/25 12:33

編集2016/10/26 05:01

またまた、タブの質問です…。

フラグメントクラスの中で、「onViewCreated」の中で、listViewに表示する為のデータをDBから取ってくる処理(updateData)を書いているのですが、これが画面遷移すると真っ白なページになって、表示されていたタブページも真っ白になってしまいます。

今のところ推測するに、updateDataの書いている位置が悪いのかなと思うのですが、本来はどこに書くのでしょうか?

ちなみに、onResumeに書くと、起動時にエクセプションでアプリが落ちてしまいます。

よろしくお願いします。

java

1 private void updateData(final Activity activity){ 2 new Thread(new Runnable() { 3 @Override 4 public void run() { 5 collectionList = ((infoCollector) getActivity().getApplication()).getinfoCollectorDatabase().getCollectionList(tab); 6 7 activity.runOnUiThread(new Runnable() { 8 @Override 9 public void run() { 10 CollectionListAdapter adapter = (CollectionListAdapter)listView.getAdapter(); 11 if(adapter == null){ 12 adapter = new CollectionListAdapter(activity, collectionList); 13 listView.setAdapter(adapter); 14 } 15 adapter.updateCollectionList(collectionList); 16 } 17 }); 18 } 19 }).start(); 20 }

テストクラスを追記↓

java

1 2 3public class CountryFragment extends Fragment implements View.OnClickListener,AdapterView.OnItemClickListener{ 4 5 // シングルトンで生成 6 private static CountryFragment mCountryFragment; 7 8 private Context mContext; 9 10 private LinearLayout mRoot; 11 private ListView listView; 12 13 private static int number; 14 private static Tab tab; 15 16 private CollectionListAdapter mCollectionListAdapter; 17 private List<Collection> collectionList; 18 19 20 public static CountryFragment newInstance(int position){ 21 mCountryFragment = new CountryFragment(); 22 number = position; 23 return mCountryFragment; 24 } 25 26 public static CountryFragment getInstance(){ 27 return mCountryFragment; 28 } 29 30 // 空のコンストラクタ必要 31 public CountryFragment() {} 32 33 @Override 34 public void onAttach(Context context){ 35 super.onAttach(context); 36 mContext = context; 37 } 38 39 // ビューを生成 40 @Override 41 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceSate){ 42 return inflater.inflate(R.layout.fragment_all,container,false); 43 } 44 45 @Override 46 public void onResume(){ 47 super.onResume(); 48 } 49 50 51 // こっちもビューを生成 52 @Override 53 public void onViewCreated(View view, Bundle savedInstanceState){ 54 super.onViewCreated(view, savedInstanceState); 55 56 // View初期化 57 mRoot = (LinearLayout)view.findViewById(R.id.root); 58 59 listView = (ListView) view.findViewById(R.id.list_collection); 60 listView.setOnItemClickListener(this); 61 listView.setOnCreateContextMenuListener(this); 62 ListView listView = (ListView)getView().findViewById(R.id.list_collection); 63 CollectionListAdapter adapter = (CollectionListAdapter)listView.getAdapter(); 64 collectionList = testView(number); 65 66 if(adapter == null){ 67 adapter = new CollectionListAdapter(getActivity(), collectionList); 68 listView.setAdapter(adapter); 69 } 70 adapter.updateCollectionList(collectionList); 71 } 72 73public List<Collection> testView(int number){ 74 List<Collection> collectionList = new ArrayList<>(); 75 76 if(number == 0){ 77 Collection collection1 = new Collection(0); 78 collection1.setName("テスト1"); 79 collection1.setType1(1); 80 collection1.setAmazon("http://xxxxxx.com"); 81 collectionList.add(collection1); 82 }else if(number ==1){ 83 Collection collection1 = new Collection(0); 84 collection1.setName("テスト1"); 85 collection1.setType1(1); 86 collection1.setAmazon("http://xxxxxx.com"); 87 collectionList.add(collection1); 88 Collection collection2 = new Collection(0); 89 collection2.setName("テスト2"); 90 collection2.setType1(2); 91 collection2.setAmazon("http://xxxxxx2.com"); 92 collectionList.add(collection1); 93 }else if(number==2){ 94 Collection collection1 = new Collection(0); 95 collection1.setName("テスト1"); 96 collection1.setType1(1); 97 collection1.setAmazon("http://xxxxxx.com"); 98 collectionList.add(collection1); 99 Collection collection2 = new Collection(0); 100 collection2.setName("テスト1"); 101 collection2.setType1(2); 102 collection2.setAmazon("http://xxxxxx.com"); 103 collectionList.add(collection2); 104 Collection collection3 = new Collection(0); 105 collection3.setName("テスト1"); 106 collection3.setType1(3); 107 collection3.setAmazon("http://xxxxxx.com"); 108 collectionList.add(collection3); 109 }else{ 110 Collection collection1 = new Collection(0); 111 collection1.setName("テスト1"); 112 collection1.setType1(1); 113 collection1.setAmazon("http://xxxxxx.com"); 114 collectionList.add(collection1); 115 Collection collection2 = new Collection(0); 116 collection2.setName("テスト1"); 117 collection2.setType1(2); 118 collection2.setAmazon("http://xxxxxx.com"); 119 collectionList.add(collection2); 120 Collection collection3 = new Collection(0); 121 collection3.setName("テスト1"); 122 collection3.setType1(3); 123 collection3.setAmazon("http://xxxxxx.com"); 124 collectionList.add(collection3); 125 Collection collection4 = new Collection(0); 126 collection4.setName("テスト1"); 127 collection4.setType1(4); 128 collection4.setAmazon("http://xxxxxx.com"); 129 collectionList.add(collection4); 130 } 131 132 return collectionList; 133 134 }

呼び出し元

java

1 @Override 2 public Fragment getItem(int position) { 3 return CountryFragment.newInstance(position); 4 }

ここのpositionにきている値がおかしい。

テストコードを追記しました。

==修正コード==

Fragment Class側

java

1 public void onViewCreated(View view, Bundle savedInstanceState){ 2 super.onViewCreated(view, savedInstanceState); 3 4 // View初期化 5 mRoot = (LinearLayout)view.findViewById(R.id.root); 6 7 listView = (ListView) view.findViewById(R.id.list_collection); 8 listView.setOnItemClickListener(this); 9 listView.setOnCreateContextMenuListener(this); 10 ListView listView = (ListView)getView().findViewById(R.id.list_collection); 11 CollectionListAdapter adapter = (CollectionListAdapter)listView.getAdapter(); 12 13 Bundle bundle = getArguments(); 14 collectionList = testView(bundle.getInt("position")); 15 if(adapter == null){ 16 adapter = new CollectionListAdapter(getActivity(), collectionList); 17 listView.setAdapter(adapter); 18 } 19 adapter.updateCollectionList(collectionList); 20 }

呼び出し元

java

1 @Override 2 public Fragment getItem(int position) { 3 4 Bundle bundle = new Bundle();; 5 6 bundle.putInt("position",position); 7 8 CountryFragment fragment = new CountryFragment(); 9 fragment.setArguments(bundle); 10 11 getFragmentManager().beginTransaction().add(R.id.container,fragment,"TAG").commit(); 12 13 return fragment; 14 }

これだと、 getFragmentManager().beginTransaction().add(R.id.container,fragment,"TAG").commit();のfragmentにエラーメッセージが出ていて、コンパイルが通りません。
エラーメッセージは「Wrong 2nd argument type. Found: 'testproject.fragment.CountryFragment', required: 'android.app.Fragment」です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yona

2016/10/25 12:44

なぞなぞじゃないんだからコードを載せましょう。
guest

回答1

0

ベストアンサー

別画面に遷移後、バックキーまたはActivity#finishで戻ってきているんですね?
そうすると画面が白くなることはないと思います。
それ以外の方法ならもっと違う原因ですね。
まずは、updateDataの冒頭でログ出力し、呼ばれているかを確認しましょう。

また、onResumeで読んだときのエラーログを追記してください。

自作したクラスをなにも説明せずに質問に載せるのは良くないですよ。

投稿2016/10/26 00:58

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

SmartBuzz

2016/10/26 02:44 編集

現状としては、1つ目のタブが2つ目のタブ表示になる→2つ目のタブ表示は正しい→3つ目4つ目も5つ目も正しい→2つ戻ると5つ目の表示になる→それ以降全部5つ目の表示になる という現象です。 質問を記載した時はデータの表示がおかしいと考えてこの質問を書きましたが、追記したテストコードを元に調べていくと、アクティビティのgetItemにきてる引数のpositionがおかしい事がわかりました。positionが呼ばれて引数としてきているpositionは正しいのですが、その先に進んでFragmentクラスの中にいくと、上記に書いた順番、1→1→2→3→4→5→5→5…となります。 自作したクラス(データ呼び出す部分だけテストコード)を記載しました。呼び出し元コードも追記しました。
yona

2016/10/26 03:12

staticの意味を理解していますか?どこからでも呼び出せる便利な機能くらいにしか思っていませんか? staticは全てのインスタンスで共通の値となります、newInstanceをした時に書き換えられるので一番最後にnewInstanceをしたときの値で画面を表示するので、意図しない表示になります。 Fragmentにデータを渡すときはsetArgumentメソッドを使って渡しましょう。
SmartBuzz

2016/10/26 03:54

なるほど…。 つまり、呼び出し元の方で、データをBundleに入れて、setArgumentsでfragmentに値を入れてreturnする Fragment fragment =CountryFragment.newInstance(position); Bundle bundle = new Bundle(); bundle = collectionList; return fragment.setArguments(bundle); こんな感じで入れてあげないといけないという事でしょうか?
yona

2016/10/26 04:04

そうです、これはテンプレートでもこのような実装になっています。 こうしないと、Fragmentの再生成時に値が消える可能性があります。
SmartBuzz

2016/10/26 04:18

ちなみに、DBに情報を取りに行ったりというのも呼び出し元のほうでしておいて、Fragmentクラスのほうでは、情報取りに行ったりとかそのような処理は好ましくなく、Viewの処理だけにしたほうがいいんでしょうか?
yona

2016/10/26 05:13

アプリケーション全体の設計によります。 Activityがデータを管理するか、Fragmentが自分で使うデータは管理するかで異なります。
SmartBuzz

2016/10/26 05:15

なるほど…。ちなみに、先ほど修正コードを追記したのですが、fragmentでimportしているFragmentの種類が違うとエラーが出るのですが、全て「import android.support.v4.app.Fragment;」をimportしているのに、このようなエラーが出る場合は、どういった対処法があるのでしょうか…?
SmartBuzz

2016/10/26 05:40

ものはためしに「getFragmentManager().beginTransaction().add(R.id.container,fragment,"TAG").commit();」の列を消したら、やりたかったことができました! ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問