簡體   English   中英

從 Alertdialog EditText 保存文本並顯示到 Recyclerview 中並永久保存

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

當用戶將文本放入編輯文本並單擊“確定”並顯示到回收站視圖時,我想保存文本。 永遠,不僅僅是一次。

  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();
            }
        }
    });

和適配器

 @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);

請教我如何做到這一點。 祝你有美好的一天。

據我了解您的問題,您想保存文本並在 RecyclerView 中顯示所有文本。 只做兩件事

  1. 使用 SQLite 創建數據庫和表,或者您可以使用 Room Library 並將文本值保存在數據庫表中。
  2. 從同一個數據庫表中獲取所有值並創建一個適配器以在 RecyclerView 中顯示這些值。

您可以通過以下鏈接了解 Room Room with RecyclerView的基本示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM