在Fragment

时间:2015-12-01 17:51:43

标签: java android android-fragments

目前,我在一个片段中有一个RecyclerView的应用程序。我目前的问题是我无法向此RecyclerView添加元素。我目前正在使用DialogFragment来获取用户的输入,当用户按下“创建”按钮时,MainActivity中的方法被称为

CreateRecipeFragment DialogFragment class:

public class CreateRecipeFragment extends DialogFragment {

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

    EditText newName, newIngredients, newMethod, newNotes;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        final View view = inflater.inflate(R.layout.fragment_create_recipe, null);

        // Pass null as the parent view because its going in the dialog layout

        Bundle mArgs = getArguments();
        final String parentBook = mArgs.getString("parentBook");

        builder.setView(view)
                // Add action buttons
                .setPositiveButton("Create Recipe", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                        newName = (EditText) view.findViewById(R.id.editTitle);
                        newIngredients = (EditText) view.findViewById(R.id.editIngredients);
                        newMethod = (EditText) view.findViewById(R.id.editMethod);
                        newNotes = (EditText) view.findViewById(R.id.editNotes);

                        String recipe_name = newName.getText().toString();
                        String recipe_ingredients = newIngredients.getText().toString();
                        String recipe_method = newMethod.getText().toString();
                        String recipe_notes = newNotes.getText().toString();

                        Recipe recipe = new Recipe(recipe_name, "Description", recipe_ingredients, recipe_method, recipe_notes, parentBook);

                        Log.d("Name:  ", recipe_name);
                        MainActivity callingActivity = (MainActivity) getActivity();
                        callingActivity.addRecipeFromFragment(recipe);
                        dialog.dismiss();

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        CreateRecipeFragment.this.getDialog().cancel();
                    }
                });

        // Create the AlertDialog object and return it
        return builder.create();
    }

    public void onClick(DialogInterface dialog, int position) {


    }

    }

片段中onClick

RecyclerView侦听器
fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CreateRecipeFragment createRecipeFragment = new CreateRecipeFragment();
            MainActivity callingActivity = (MainActivity) getActivity();
            createRecipeFragment.show(callingActivity.getSupportFragmentManager(), "DialogBOX2");
            recipes.add(callingActivity.tempRecipe);
            m2Adapter.notifyDataSetChanged();
            // m2Adapter is the adapter for the RecyclerView in the fragment
        }
    });

MainActivity

中的添加方法
public Recipe tempRecipe;
public void addRecipeFromFragment(Recipe recipe) {
    dbManager.addRecipe(recipe);
    tempRecipe = recipe;

}
addRecipe类(SQLite)

中的

DBManager方法

public void addRecipe(Recipe recipe) {

    ContentValues values = new ContentValues();
    values.put(COLUMN_RECIPE_NAME, recipe.getRecipeTitle());
    values.put(COLUMN_RECIPE_DESCRIPTION, recipe.getRecipeDescription());
    values.put(COLUMN_RECIPE_INGREDIENTS, recipe.getIngredients());
    values.put(COLUMN_RECIPE_METHOD, recipe.getMethod());
    values.put(COLUMN_RECIPE_NOTES, recipe.getNotes());
    if (COLUMN_IMAGE_ID != null) {
        values.put(COLUMN_IMAGE_ID, recipe.getImageId());
    }
    values.put(COLUMN_PARENT_BOOK, recipe.getParentBook());

    SQLiteDatabase db = this.getWritableDatabase();
    db.insert(TABLE_RECIPES, null, values);
    db.close();
}

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,当您在其中使用警报对话框时,创建对话框片段没有任何意义。您可以从对话框片段中删除“警报”对话框创建代码,并根据需要为对话框片段设置自定义布局。这与片段相同。 其次,你应该使用接口从片段到活动进行通信。看看下面的链接 http://developer.android.com/training/basics/fragments/communicating.html http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/

第三个在活动中全局拥有Recycler适配器,并在适配器中有公共方法,它将新添加的项添加到列表中添加调用notifyItemInserted(0); 我还可以看到你在db中插入数据。如果是这样,您可以在适配器中编写公共方法并从db获取所有收件人并将其放入adpater中维护的List模型中并调用notifyDataSetChanged