简体   繁体   English

从 Alertdialog EditText 保存文本并显示到 Recyclerview 中并永久保存

[英]Save text from Alertdialog EditText and show into Recyclerview and save it forever

I want to save text when user put text in edit text and click "ok" and show to recycler view.当用户将文本放入编辑文本并单击“确定”并显示到回收站视图时,我想保存文本。 forever, not just one time.永远,不仅仅是一次。

  AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
                builder1.setTitle("story name");
                final EditText editText = new EditText(getContext());
                editText.setHint("Name your story");
                final LinearLayout linearLayout1 = new LinearLayout(getContext());
                linearLayout1.setPadding(10, 10, 10, 10);
                linearLayout1.setOrientation(LinearLayout.VERTICAL);
                builder1.setView(linearLayout1);
                builder1.setView(editText);

                builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String text = editText.getText().toString().trim();
                        if (TextUtils.isEmpty(text)){
                            Toast.makeText(getContext(), "Please write story name...", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            // save text
                        }


                    }
                });

                builder1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder1.create().show();
            }
        }
    });

And Adapter和适配器

 @Override
public void onBindViewHolder(@NonNull StoryHolder holder, int position) {
    String story_name = story.get(position).getStory_name();
    String date = story.get(position).getDate();

    holder.storyText.setText(story_name);
    holder.storyDate.setText(date);

Please teach me how to do this.请教我如何做到这一点。 Good day to you.祝你有美好的一天。

As I understand with your question you want to save the text and show all the text in a RecyclerView.据我了解您的问题,您想保存文本并在 RecyclerView 中显示所有文本。 Just do two things只做两件事

  1. Create a database and table using SQLite OR you can use Room Library and save the text value in the database table.使用 SQLite 创建数据库和表,或者您可以使用 Room Library 并将文本值保存在数据库表中。
  2. Fetch all the values from the same database table and create an adapter to show the values in RecyclerView.从同一个数据库表中获取所有值并创建一个适配器以在 RecyclerView 中显示这些值。

You can use the following link to understand the basic example of Room Room with RecyclerView您可以通过以下链接了解 Room Room with RecyclerView的基本示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM