一个如何使用java从android中的material design复制简单对话框示例的示例

xxslljrj  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(292)

我很难理解如何进行这里介绍的简单对话框示例:https://material.io/components/dialogs/android#types

我想做同样的事情(项目列表和添加另一个项目的按钮)。
我已经做了物品清单。。

public void onChangeHouseShare(MenuItem item) {
        Log.i(TAG, token);
        HouseshareAPI.getInstance(this).getHouseshares(
                response -> {
                    Log.i(TAG, response.toString());
                    try {
                        List<Houseshare> houseshareList = new ArrayList<>(response.length());
                        CharSequence[] items = new CharSequence[response.length() + 1];

                        for (int i = 0; i<response.length(); i++) {
                            JSONObject json = response.getJSONObject(i);
                            JSONObject userJson = json.getJSONObject("manager");
                            User manager = new User(
                                    userJson.getInt("userId"),
                                    userJson.getString("pseudo"),
                                    userJson.getString("email"),
                                    userJson.getString("firstname"),
                                    userJson.getString("lastname"));

                            houseshareList.add(new Houseshare(json.getInt("houseshareId"), json.getString("name"), manager));
                            items[i] = json.getString("name");
                        }

                        Log.i(TAG, "JSON Array size " + response.length());
                        Log.i(TAG, "Array size " + items.length);
                        Log.i(TAG, "List size " + houseshareList.size());
                        items[items.length - 1] = "Create a new houseshare";

                        AtomicReference<Houseshare> selectedHouseshare = new AtomicReference<>();
                        new MaterialAlertDialogBuilder(this)
                                .setTitle("Choose an house share here")
                                .setItems(items, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Log.i(TAG, String.valueOf(which));

                                        selectedHouseshare.set(houseshareList.get(which));

                                        SharedPreferences sharedPref = MainActivity.this.getSharedPreferences(MainActivity.this.getString(R.string.global_preference_key_file), Context.MODE_PRIVATE);
                                        SharedPreferences.Editor editor = sharedPref.edit();
                                        editor.putString(MainActivity.this.getString(R.string.default_selected_houseshare_id), String.valueOf(selectedHouseshare.get().getHouseshareId()));
                                        editor.apply();

                                        MainActivity.this.invalidateOptionsMenu();
                                    }
                                })
                                .show();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                },
                errorResponse -> {
                    if (errorResponse.networkResponse != null) {
                        String jsonError = new String(errorResponse.networkResponse.data);
                        Log.e(TAG, jsonError);
                    } else {
                        errorResponse.printStackTrace();
                        Toast.makeText(
                                this,
                                "Network error, server unreachable. Please try again later.",
                                Toast.LENGTH_LONG).show();

                        Log.e(TAG, "Network error, server unreachable.");
                    }
                }
        );
    }

但接下来我该怎么办呢?如何使用文本条调用片段或其他对话框来编写新项?有什么好的例子可以证明这一点吗?
顺便说一下,我使用移动导航来管理片段和导航抽屉。
谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题