如何在Android中的对话框片段中刷新片段?

时间:2020-04-17 23:29:07

标签: java android android-studio android-fragments

我对如何更新对话框片段中的片段有疑问。

这是我的应用程序的图像:it is my application

当我单击过滤器菜单按钮时,将显示一个新的对话框片段,其中包括一个广播组。

dialog

当我单击“确定”按钮时,我想更新包含位置列表的片段。

这是PlaceActivity的代码,其中包含PlaceFragment:

公共类PlaceActivity扩展了AppCompatActivity {

IntPtr

}

这是PlaceFragment类的代码:

公共类PlaceFragment扩展Fragment {

// assign frame layout.
FrameLayout frameLayout;
// assign bottom navigation.
BottomNavigationView bottomNavigationView;

//assign and initialize whole of fragments.
FavoriteFragment favoriteFragment = new FavoriteFragment();
MyPlanFragment myPlanFragment = new MyPlanFragment();
PlaceFragment placeFragment = new PlaceFragment();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.place_activity);

    // initialize views by id.
    frameLayout = findViewById(R.id.frame_place_activity);
    bottomNavigationView = findViewById(R.id.bottom_navigation_view);


    // Set book fragment as initial fragment.
    setFragment(placeFragment);
    bottomNavigationView.setSelectedItemId(R.id.bottom_nav_place);

    // Set on click listener on bottom navigation items.
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.bottom_nav_plan:
                    setFragment(myPlanFragment);
                    return true;
                case R.id.bottom_nav_place:
                    setFragment(placeFragment);
                    return true;
                case R.id.bottom_nav_favorite:
                    setFragment(favoriteFragment);
                    return true;
                default:
                    return false;
            }
        }
    });

}


/**
 * set frame layout to the fragment of argument.
 * @param fragment is used in order to inflate frame layout.
 */
private void setFragment(Fragment fragment) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame_place_activity,fragment);
    fragmentTransaction.commit();
}

}

这是ExampleDialog类的代码:

公共类ExampleDialog扩展了AppCompatDialogFragment {

public static final String TAG = PlaceFragment.class.getName();

// String which stores json string returnable.
String jsonResponse = GooglePlaceJson.MALLS_JSON_RESPONSE;

// Shared Preferences is used to return back the id of selected item.
SharedPreferences sharedPreference;

// Empty view is shown when the list view does not contain any items.
TextView emptyView;

public PlaceFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_place, container, false);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
}

// called when view is created.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //ListView for showing a list of places
    ListView listView = view.findViewById(R.id.list);

    //TextView for showing a empty list view.
    emptyView = view.findViewById(R.id.empty_text_view);

    //Create an object of QueryUtils in order to get data from it.
    QueryUtils queryUtils = new QueryUtils(jsonResponse);

    //Adding a list of the places.
    ArrayList<Place> places = queryUtils.extractPlaces();

    //Array adapter used to get the source data and the layout of the list item.
    final PlaceAdapter placeAdapter = new PlaceAdapter(getContext(), places);

    // Set the adapter on the {@link ListView}
    // so the list can be populated in the user interface
    listView.setAdapter(placeAdapter);

    // Set the empty view to list view, cause it will show when there is not item on the list view.
    listView.setEmptyView(emptyView);

    // Set clickable method on item list view.
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // Find the current place that was clicked on
            Place currentPlace = placeAdapter.getItem(position);

            // Intent used to go to second activity.
            Intent goPlaceDetail = new Intent(getContext(), DetailedPlace.class);
            // Pass current Activity to another Activity.
            goPlaceDetail.putExtra("currentPlace",currentPlace);
            startActivity(goPlaceDetail);
        }
    });

}

// Create a menu then add it to the fragment.
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    inflater.inflate(R.menu.main_menu,menu);
}

// Set on click listener on the menu items.
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.place_menu_filter_:
            // Assign shared preference.
            sharedPreference= getContext().getSharedPreferences(ExampleDialog.MY_PREFS_NAME,MODE_PRIVATE);
            // Open dialog to filter places.
            openDialog();
            // Get the checked text
            String checkedTextItem = sharedPreference.getString(ExampleDialog.CHECKED_ITEM_TEXT_KEY,"");
            if (checkedTextItem.equals(ExampleDialog.RESTAURANT)){
                jsonResponse = GooglePlaceJson.RESTAURANT_JSON_RESPONSE;
            } else if(checkedTextItem.equals(ExampleDialog.MALL)){
                jsonResponse = GooglePlaceJson.MALLS_JSON_RESPONSE;
            }

            this.onViewCreated(getView().findViewById(R.id.list),null);
            return true;
        case R.id.action_settings:
            // Go settings activity when the settings menu item was clicked.
            Intent goSettings = new Intent(getContext(), SettingsActivity.class);
            // Start activity to go to second activity.
            startActivity(goSettings);
            return true;
        default:
            return false;
    }
}


/**
 * Method is used to open dialog.
 */
private void openDialog() {
    // Create an object of the custom dialog.
    ExampleDialog exampleDialog = new ExampleDialog();
    exampleDialog.show(getFragmentManager(),"filter place dialog");

}

}

1 个答案:

答案 0 :(得分:0)

在致电exampleDialog.show(getFragmentManager(),"filter place dialog");之前

exampleDialog.setTargetFragment( PlaceFragment.this, MY_DATA_REQUEST_CODE)

然后在通过onClick()方法从单选按钮获取数据之后,在 ExampleDialog 类中,将数据作为额外内容放入一个可能称为intentWithDialogData的意图中,然后在调用片段( PlaceFragment )中调用getTargetFragment().onActivityResult(MY_DATA_REQUEST_CODE, RESULT_CODE, intentWithDialogData)覆盖 onActivityResult(),以从传递的意图中接收数据