简体   繁体   中英

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. 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.
  2. Fetch all the values from the same database table and create an adapter to show the values in RecyclerView.

You can use the following link to understand the basic example of Room Room with RecyclerView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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