-2

HELP! Okay so I have been designing an android app for quite some time now and I have been manually putting all this data into strings and then just pulling them up in my layouts, but then a friend of mine suggested I put all the necessary data into a database and then just pull it out of there on each activity....sounds good....Accept I have been reading tutorial after tutorial on how this works and it seems much harder than just making lots and lots of strings and the examples in the tutorials each serve their own purpose which is not mine and dose not make understanding any easier for me. All I will be needing this database to do is read and display the info where I want it on the layouts. I created this database with SQLite Database Browser.

Database structure:

Name - fishindex_db
Tables - fish, states, reg
Rows:
fish - _id, name, desc, loc
states - _id, name, abbr, updated
reg - _id, name, size, season, quantity, notes

so now say I want to display all the content from primary key (_id) 12 from the reg table in a layout list view how is this done? need .java and .xml code example please.

4

2 に答える 2

5

These are two tutorials you can use to get you up and running in terms of what you want to achieve:

Hope this helps.

于 2013-01-24T05:05:16.347 に答える
0

Take a look at the code to connect to fishindex_db

public class SQLiteHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "fishindex_db.db";

public static final String TABLE_NAME = "fish";
public static final String _id = "id";
public static final String name = "name";
public static final String desc = "desc";

private SQLiteDatabase database;

SQLiteHelper sQLiteHelper = new SQLiteHelper(MainActivity.this);
public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//this is to get all records in fish table
public ArrayList<FishModel> getAllRecords() {
    database = this.getReadableDatabase();
    Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null);

    ArrayList<FishModel> fishes= new ArrayList<FishModel>();
    FishModel fishModel;
    if (cursor.getCount() > 0) {
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();

            fishModel= new FishModel();
            fishModel.setID(cursor.getString(0));
            fishModel.setName(cursor.getString(1));
            fishModel.setLastName(cursor.getString(2));

            fishes.add(fishModel);
        }
    }
    cursor.close();
    database.close();
    return contacts;
}
于 2016-10-16T15:41:49.423 に答える