开发者

Seeking guidance: data storage decision

开发者 https://www.devze.com 2023-03-01 05:26 出处:网络
I apologize in advance if this question is too broad or too \"it-depends.\" Basically, I need to make a decision about how to store and access data in my app. The app works very much like flashcards

I apologize in advance if this question is too broad or too "it-depends."

Basically, I need to make a decision about how to store and access data in my app. The app works very much like flashcards; the "front" of the card presents the topic and the "back" presents the details/explanation. At first it seemed like a SQLite database would work best for this typ开发者_JAVA百科e of structure (and maybe it really is, I just don't know) because the data is static and this model works well with the rows and columns structure of a db. (btw, as of now, I'm using openCSV to parse the csv files containing my cards. Thought it was easier than SQLite...)

My issue (finally) is that I want to be able to display images for some of the data items. Some cards, for example, should display a corresponding image. Is this something that I can do with a SQLite db? Like, have one column store the path to an image....? Maybe what I'm asking is really basic, but I just haven't seen too many examples to really have a good sense of the design options out there.

I might also be confused about how I would dynamically change my views based on whether there is an image available. Maybe that's just an issue of dynamically creating an imageview whose source is the file that the db points to...

In summary, I'd really appreciate some guidance on how I can fetch and display text data along with images when they're available, whether it be in SQLite or some other way.

Thanks!


If all this data is being shipped with the app, I'd suggest just keeping everything stored as resources. You can have string arrays for the topics and details, and you can store images either as drawable resources or as assets. In the latter case, you could store the asset names as another string array resource. (In the former case, you'd have to build a map from each card to the resource identifier. This is, unfortunately, one area in which the Android resources architecture doesn't shine.)

If you want to use SQLite, it has BLOB fields in which you could store the images themselves, or int fields in which you could store image resource identifiers. Take a look at the searchable dictionary sample project for how to build an SQLite data base from resource data.


Sorry but this really is a 'it depends' kind of topic.

Unless you're storing a large number of rows of data (around the order of 10 000) then an XML file would be your best bet. In a Train Timetable app I recently wrote we went with a XML SAX parser loading a 14 000 record database to memory and it took no more than 2 seconds on an HTC Hero, so even for large databases its pretty fast.

The SQLite option is preferable only if you want to make use of relationships that come with a database structure. It is better at handling large numbers of rows but terrible at handling images.

Since you're flash cards are not relational I would recommend an xml file, using the xmlSax parser, and a folder of images within your assets folder. You could even run the images through pngCrunch to save some space.

XML is very flexible, below is an example of what your xml file could look like. Check out http://www.w3schools.com/schema/ for more information on writing an xml schema.

<?xml version="1.0" encoding="UTF-8"?>
<cards>
  <card title="card 1" topic="atopic" image="image file name">
    <front>Lots of text</front>
    <back>Lots of text</back>
  </card>
  <card ..>
    ..
  </card>
</cards>
0

精彩评论

暂无评论...
验证码 换一张
取 消