java - 从 Alertdialog EditText 保存文本并显示到 Recyclerview 并永久保存
问题描述
当用户将文本放入编辑文本并单击“确定”并显示到回收站视图时,我想保存文本。永远,不只是一次。
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 中显示所有文本。只做两件事
- 使用 SQLite 创建数据库和表,或者您可以使用 Room Library 并将文本值保存在数据库表中。
- 从同一个数据库表中获取所有值并创建一个适配器以在 RecyclerView 中显示这些值。
您可以通过以下链接了解Room Room with RecyclerView的基本示例
推荐阅读
- javascript - javascript对象空白属性值在asp.net web api中转换为null
- assembly - 无法更改 TASM 中角色的颜色
- c++ - 为什么演绎指南适用于 C 风格的数组参数,但不适用于 std::array 参数?
- powerbi - 该列不能有空白值
- command - 编写一个程序来读取 /proc 文件系统并为所有进程打印以下内容
- nuxt.js - 如何使用带有 Nuxt.js 的 FormData 正确上传文件?
- python - 记录器在其他地方实例化到 __main__ 时没有收到来自它调用的模块的消息?
- android - 如何删除 Android Material Slider 周围的间距?
- visual-studio-code - vscode-go,填充结构命令不起作用
- c# - 用于设置嵌入互操作类型的预构建事件 = false 对于所有带有单词 interop 的引用