如何解决如何存储和检索以在 Room 数据库上显示表情符号 (Android Studio)
我正在使用带有 Java 语言的 Room 数据库构建情绪跟踪器 android 应用程序。 我打算使用 android 内置的表情符号让用户输入他们想要的表情符号,但我不确定如何将它存储在 Room db 中,然后检索数据以显示在另一个片段中?
如果有人能就此提供一些指导或附上相关的教程视频链接,我将不胜感激,因为我对移动应用程序开发还是个新手
解决方法
就 Room 而言,您只需要存储一些知道表情符号是什么的方法,也许是表情符号的 Unicode。
例如笑脸是0x1F600,也许是描述性的。然后,您可以设置 TextView,例如,合并表情符号。
也许你可以有一个只包含表情符号的表格,然后存储对这个表格的引用(有关系)。
下面是一个非常简单的 Room 示例。
结果将是显示在 ListView 中的表情符号列表。
这个例子有一个表,其中存储了表情符号代码以及表情符号的描述。该表已命名为 emoji,实体类为 EmojiTable,如下所示:-
@Entity(tableName = "emoji")
class EmojiTable {
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "_id")
Long id;
@ColumnInfo(name = "emoji_code",index = true)
String emoji_code;
@ColumnInfo(name = "emojidesc")
String emoji_desc;
// Default constructor
EmojiTable(){}
// Alternative Constructor
@Ignore
EmojiTable(String emoji_code,String emoji_desc) {
this.emoji_code = emoji_code;
this.emoji_desc = emoji_desc;
}
// Override default toString method to return the emoji followed by it's decsription
@Ignore
public String toString() {
return new String(Character.toChars(Integer.decode(emoji_code))) + " " + emoji_desc;
}
}
- 3 列
- 唯一标识符的 _id 列
- 用于表情符号十六进制代码的 emoji_code 列
- 用于描述表情符号的 emoji_desc 列
- 请注意,toString 方法已被覆盖,以返回一个包含表情符号及其说明的字符串。这个是用来设置ListView中的TextView的。
- 否则将使用默认的 toString 方法来显示对象。
为了配合表情符号表,数据库操作的 DAO AllDao :-
@Dao
interface AllDao {
// Insertion of a row
@Insert(entity = EmojiTable.class,onConflict = OnConflictStrategy.IGNORE)
long insertEmoji(EmojiTable e);
//Deletion of ALL rows
@Query("DELETE FROM emoji")
int deleteAll();
// Get ALL rows
@Query("SELECT * FROM emoji")
List<EmojiTable> getAllEmojis();
// Get a row by the id passed to the method
@Query("SELECT * FROM emoji WHERE emoji._id = :id")
EmojiTable getEmojiById(Long id);
// Get the number of rows (used to detect changes)
@Query("SELECT count(*) FROM emoji")
Long getRowsInEmojiTable();
}
@Database 抽象类MyDatabase
@Database(entities = {EmojiTable.class},version = 1)
abstract class MyDatabase extends RoomDatabase {
abstract AllDao getAllDao();
}
一个类,即定义表情符号/表情符号主列表的EmojiList。
- 使用此更改(如果表情符号的更改数量)允许更改存储在数据库中的表情符号(应部署新版本的应用)。
.
class EmojiList {
public static final EmojiTable[] EMOJIS = {
new EmojiTable("0x1F600","Grinning Face"),new EmojiTable("0x1F602","Unknown"),new EmojiTable("0x1F911","Money-Mouth Face")
};
}
- 定义了 3 个表情符号
包含用于显示表情符号列表的 ListView 的布局:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
<ListView
android:id="@+id/emojilist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/design_default_color_secondary"/>
</LinearLayout>
- 这里没有什么特别的,只是通过背景颜色来查看列表的位置。
最后把它们放在一起MainActivity
public class MainActivity extends AppCompatActivity {
MyDatabase db;
AllDao dao;
List<EmojiTable> emojiList_list;
ListView emojiList_listview;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the MyDatabase object with a built Room Database
db = Room.databaseBuilder(this,MyDatabase.class,"mydb")
.allowMainThreadQueries()
.build();
// Instantiate the dao interface
dao = db.getAllDao();
// Check to see if the emoji's stored equals the master list
// If not rebuild the emjoi table
if (dao.getRowsInEmojiTable() != EmojiList.EMOJIS.length) {
dao.deleteAll(); // Delete all existing rows
for(EmojiTable e: EmojiList.EMOJIS) {
dao.insertEmoji(e);
}
}
// Very simple list of all emojis
emojiList_list = dao.getAllEmojis(); //
emojiList_listview = this.findViewById(R.id.emojilist);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,emojiList_list);
emojiList_listview.setAdapter(adapter);
}
}
结果
运行时应用程序看起来像:-
数据库(通过数据库检查器):-
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。