Android应用中键值对应字符串的存储与高效访问方案咨询
Hey there! Since you already have ListView implementation sorted, let's dive straight into straightforward, efficient ways to store your (Name, Type, Shortname) data—perfect for your use case where you only show Name/Type in the UI and need Shortname on tap.
1. Data Class + In-Memory List (Best for Small, Static Data)
This is the simplest and fastest option for 200 entries—no extra dependencies, zero overhead.
First, define a data class to hold your three fields (example in Kotlin; use a POJO if you're on Java):
data class DataItem( val name: String, val type: String, val shortname: String )
Then initialize a list with your 200 entries:
val staticItems = listOf( DataItem("John Doe", "User", "JD001"), DataItem("Jane Smith", "Admin", "JS002"), // ... add all 200 entries here )
When setting up your ListView adapter, pass this list. In your item click listener, just grab the shortname from the clicked DataItem directly.
Pros: Lightning-fast access, minimal code, no setup required.
Cons: Data is hardcoded—you'll need to recompile if you update entries.
2. Resource File (JSON/XML) – Separate Data from Code
If you want to keep data out of your codebase (easier to update without recompiling), use a raw resource file.
Example with JSON:
- Create
res/raw/items.jsonand populate it with your data:
[ {"name": "John Doe", "type": "User", "shortname": "JD001"}, {"name": "Jane Smith", "type": "Admin", "shortname": "JS002"} ]
- Read and parse the file into your
DataItemlist using a JSON parser like Gson or Android's built-inJsonReader:
val inputStream = resources.openRawResource(R.raw.items) val jsonString = inputStream.bufferedReader().use { it.readText() } val staticItems = Gson().fromJson(jsonString, Array<DataItem>::class.java).toList()
Then use this list with your ListView exactly like the in-memory approach.
Pros: Data is editable without code changes, cleaner separation of concerns.
Cons: Slight parsing overhead (negligible for 200 entries).
3. Room Database (For Future Scalability)
If you anticipate your dataset growing beyond 200 entries, or if you need query/filter functionality later, Room is a lightweight, easy-to-implement SQLite wrapper.
- Define an Entity class:
@Entity(tableName = "items") data class DataItem( @PrimaryKey(autoGenerate = true) val id: Int = 0, val name: String, val type: String, val shortname: String )
- Create a DAO (Data Access Object) to fetch all items:
@Dao interface ItemDao { @Query("SELECT * FROM items") suspend fun getAllItems(): List<DataItem> }
- On app launch, populate the database once (check if it's empty first to avoid duplicates). Then fetch the list and bind it to your ListView.
Pros: Structured storage, supports queries/updates, scalable for larger datasets.
Cons: Slightly more setup work (worth it only if you need future flexibility).
Final Recommendation
For your current 200-entry static dataset, go with the Data Class + In-Memory List—it's the most efficient and simplest solution. If you need easy data updates, switch to the resource file approach. Reserve Room for when your data needs grow.
内容的提问来源于stack exchange,提问作者Wu Wenjun

