別のフラグメントのデータを使用してListviewにデータを挿入しようとしています。フラグメント内のリストビューに別のフラグメントのデータを取り込む方法は?
他のフラグメントからデータを取得できますが、リストビューオブジェクトを作成しようとするとnullが返されます。
その結果、アプリがクラッシュしています。
1つのフラグメントからユーザーからデータを取得してから、別のフラグメントのメソッドを呼び出してデータを渡しています。私は2番目のメソッドのpoplist()メソッドでリストビューオブジェクトと配列アダプタを作成しています。しかし、nullポインタの例外のために、アプリケーションがクラッシュしています。 助けてください。
public class WishListFragment extends Fragment {
ArrayList<String> wishListTitles = new ArrayList<String>();
View rootView;
ListView wishListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = getActivity();
rootView = inflater.inflate(R.layout.fragment_wish_list, container, false);
return rootView;
}
// Another fragment gets the data from user as strings and calls this method
public void popList(String str1, String str2, String str3, Context context)
{
// LibModel is a pojo, I am using its constructor to set the values.
LibModel lm = new LibModel(str1,str2,str3);
Log.w("Title:",lm.getTitle());
Log.w("Author:",lm.getAuthor());
Log.w("Language:",lm.getLang());
wishListTitles.add(lm.getTitle());
// I get this on the log, so the ArrayList is made correctly
Log.w("ArrayList:",wishListTitles.get(0));
// The listView gives Null Pointer exception and crashes the app
wishListView = (ListView) rootView.findViewById(R.id.wl_lv);
ArrayAdapter<String> wishListAdapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1,wishListTitles);
wishListView.setAdapter(wishListAdapter);
}
}
私は次のことを試してみましたが、リストビューをしながらではなくrootViewの
- 使用済みGetViewメソッドを動作しません。
- onCreateView()メソッド内でリストビューを作成しようとしましたが、listviewオブジェクトがnullの場合、nullポインタが返されます。
Nullを返すように、リストビューのアダプタを設定する方法が見つかりません。
FragmentOneからFragmenttwoにbunlde経由でデータを渡し、Fragmenttwoで取得し、adapter/Listview/recyclerviewに移入します。 – Android