简体   繁体   English

我如何在 android studio 中创建历史布局,例如最近

[英]How I can create history layout in android studio such as recent

I am building an app include edit text and button.我正在构建一个应用程序,包括编辑文本和按钮。 The user can put text in the edit text, then click button to transfer him to the browser.用户可以将文本放入编辑文本中,然后单击按钮将其传输到浏览器。 So, I would like to create other layout as recent to store the text that the user entered on the edit text.因此,我想创建其他最近的布局来存储用户在编辑文本中输入的文本。

How I can do that?我怎么能这样做? I need the logic or code that can help me!我需要可以帮助我的逻辑或代码! Also, should I have create database to store the data?另外,我应该创建数据库来存储数据吗?

Example: enter image description here示例:在此处输入图像描述

You can store data in multiple ways, and you will need to understand what is right for your use case.您可以通过多种方式存储数据,并且您需要了解哪种方式适合您的用例。 You can store data in memory, by simply creating a List with your data type and adding to it every time user will click a button, but then it will not persist between sessions with the app.您可以将数据存储在内存中,只需使用您的数据类型创建一个 List 并在每次用户单击按钮时添加到它,但是它不会在与应用程序的会话之间持续存在。

If you want the data to persist, then you would need to use permanent storage, and there are a lot of options here:如果您希望数据持久保存,则需要使用永久存储,这里有很多选项:

  • You could use Shared Preferences您可以使用共享首选项

  • You could use File System and save the data to a file您可以使用文件系统并将数据保存到文件中

  • You could use a database ie SQLite, and store data there您可以使用数据库,即 SQLite,并在那里存储数据

  • You could use external server, and get the data through REST API.您可以使用外部服务器,并通过 REST API 获取数据。

Generally, there is a good overview of data storage in Android in the documentation which also have code examples.通常,在文档中对 Android 中的数据存储有很好的概述,其中也有代码示例。

Every option comes in multiple ways to accomplish it.每个选项都有多种方式来完成它。 There are built-in solutions, and multiple libraries to help you with this task, but first of all, you will need to understand what is the predicted usage of this data.有内置的解决方案和多个库可以帮助您完成此任务,但首先,您需要了解这些数据的预测用途是什么。 IE Should user have access to the data from another device? IE 用户应该可以访问其他设备的数据吗? Should the data be available offline?数据应该离线可用吗? Will data have complex structure?数据会有复杂的结构吗? How the app can expand in the future?该应用程序将来如何扩展? etc ETC

Only by knowing this you can design how you will handle it.只有知道这一点,您才能设计如何处理它。

If you need logic or code to create view, then you will needListView , or RecyclerView , Adapter for handling the data, extra xml layout file for single item of your data.如果您需要逻辑或代码来创建视图,那么您将需要ListViewRecyclerView ,用于处理数据的适配器,用于单个数据项的额外 xml 布局文件。

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

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