またまた、タブの質問です…。
フラグメントクラスの中で、「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」です。
回答1件
あなたの回答
tips
プレビュー