选择AlertDialog选项时刷新片段

时间:2019-01-17 14:34:32

标签: android android-fragments alertdialog

我正在尝试在警报对话框中选择一个选项时刷新片段,以便新数据反映在表中。尝试了各种方法,例如此处Refresh Fragment at reload的此链接中提到的方法。但收到一个空异常。数据存储在sql db中,因此需要刷新该片段。 有人可以指出我正确的方向。

MainActivity

   TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Cinema"));
    tabLayout.addTab(tabLayout.newTab().setText("Trailers"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    final TextView mText = (TextView) findViewById(R.id.editSearch);
    listItems = getResources().getStringArray(R.array.location_item);
    mText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
            mBuilder.setTitle("Choose a location");
            mBuilder.setSingleChoiceItems(listItems, -1, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mText.setText(listItems[i]);
                    dialogInterface.dismiss();

                }

            });
            AlertDialog mDialog = mBuilder.create();
            mDialog.show();
        }

    });

PagerAdapter

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;


public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            MovieFragment tab1 = new MovieFragment();
            return tab1;
        case 1:
            TrailerFragment tab2 = new TrailerFragment();
            return tab2;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}

电影片段

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_movie, container, false);
     progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    mGridView = (GridView) view.findViewById(R.id.gridview);

    mGridAdapter = new GridAdapter(getContext(),R.layout.grid_item,movie);

    mGridView.setAdapter(mGridAdapter);
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i;

                    i = new Intent(MovieFragment.this.getActivity(),MovieDetail.class);

                    i.putParcelableArrayListExtra("array",movie);
                    i.putExtra("id",position);

                    startActivity(i);



        }
    });

    if(savedInstanceState!=null){
        movie = savedInstanceState.getParcelableArrayList(STATE_MOVIE);
        mGridAdapter.setGridData(movie);
    }
    else{
        getData();
    }

    return view;


  }

0 个答案:

没有答案