我有一个使用View Pager和Spinner的主要活动。当我更改微调器时,我希望ViewPager中的片段更新以使用新数据。
当我在Spinner中进行选择但是我一直收到NullPointerException时,我试图从Activity传递到Fragment。
每当确认Spinner选择时,我都会从Activity调用updateListView()方法。注释的代码行也会导致崩溃。
ListFragment lf = (ListFragment) mSectionsPagerAdapter.getItem(0);
lf.updateListView();
这是我的代码
public class ListFragment extends Fragment {
ArrayList<Psalm> psalms;
ListViewAdapter adapter;
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_list, parent, false);
psalms = GlobalSingleton.get(getActivity()).getPsalms().getPsalms();
adapter = new ListViewAdapter(getActivity(), psalms);
ListView l = (ListView) v.findViewById(R.id.fragmentListListView);
l.setAdapter(adapter);
return v;
}
public void updateListView(){
psalms = GlobalSingleton.get(getActivity()).getPsalms().getPsalms();
System.out.println("HERE");
((ListViewAdapter) ((ListView) v.findViewById(R.id.fragmentListListView)).getAdapter()).notifyDataSetChanged();
//adapter.notifyDataSetChanged();
}
}
主要活动
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String[] files = getAssets().list("");
ArrayList<Psalm> singPsalms = new ArrayList<Psalm>();
ArrayList<Psalm> scottishPsalter = new ArrayList<Psalm>();
for(String s: files){
if(s.split("_")[0].equals("SingPsalms")){
singPsalms.add(new Psalm(s.split("_")[1], s.split("_")[2], s.split("_")[3].replace(".txt", "")));
} else if(s.split("_")[0].equals("ScottishPsalter")){
scottishPsalter.add(new Psalm(s.split("_")[1] , s.split("_")[2], s.split("_")[3].replace(".txt", "")));
}
}
Collections.sort(singPsalms, new PsalmComparator());
GlobalSingleton.get(this).setSingPsalms(new Psalmody("Sing Psalms", singPsalms));
GlobalSingleton.get(this).setScottishPsalter(new Psalmody("Scottish Psalter", scottishPsalter));
GlobalSingleton.get(this).setSelectedPsalmody("Scottish Psalter");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Adapter
Spinner psalter = (Spinner) findViewById(R.id.spinner1);
SpinnerAdapter adapter = ArrayAdapter.createFromResource(this, R.array.psalter, android.R.layout.simple_spinner_dropdown_item);
psalter.setAdapter(adapter);
psalter.setOnItemSelectedListener( new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected = parent.getItemAtPosition(position).toString();
if (!selected.equals(GlobalSingleton.get(getApplicationContext()).getSelectedPsalmody())){
GlobalSingleton.get(getApplicationContext()).setSelectedPsalmody(selected);
System.out.println("HERE");
ListFragment lf = (ListFragment) mSectionsPagerAdapter.getItem(0);
lf.updateListView();
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = null;
if (position == 0){
fragment = new ListFragment();
} else {
fragment = new GridFragment();
}
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "List".toUpperCase(l);
case 1:
return "Grid".toUpperCase(l);
case 2:
return "Metre Seperated".toUpperCase(l);
}
return null;
}
}
}
答案 0 :(得分:0)
使用setAdapter
中onCreateView
调用{4}}的ListView的相同对象来调用notifyDataSetChanged
:
....
l = (ListView) v.findViewById(R.id.fragmentListListView);
l.setAdapter(adapter);
return v;
}
ListView l;
public void updateListView(){
//....
l.getAdapter()).notifyDataSetChanged();
//adapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
尝试以下代码。不要使用视图并使用成员列表视图。
public class ListFragment extends Fragment {
ArrayList<Psalm> psalms;
ListViewAdapter adapter;
ListView l;
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_list, parent, false);
psalms = GlobalSingleton.get(getActivity()).getPsalms().getPsalms();
adapter = new ListViewAdapter(getActivity(), psalms);
l = (ListView) v.findViewById(R.id.fragmentListListView);
l.setAdapter(adapter);
return v;
}
public void updateListView(){
psalms = GlobalSingleton.get(getActivity()).getPsalms().getPsalms();
System.out.println("HERE");
if(l != null){
l.getAdapter()).notifyDataSetChanged();
}
}