Android如何使用来自Sqlite查询的数据加载n TextViews

时间:2015-03-30 21:08:25

标签: android sqlite arraylist

我有一个sqlite数据库,其中包含一些TextView的信息,比如文本,背景颜色和已更改的TextView的ID,因为我有一个带有60个TextView的TableView。 当用户触摸其中一个时,他可以更改TextView的内容和背景颜色。 我的问题是,当我收回所有保存的TextView时,我将它们放入列表中。

Materia.java是我的对象

package com.ddz.diarioscolastico;

public class Materia {

private int _id;
private String _nome;
private int _colore;
//private int _giorno;
//private int _ora;

//Empty constructor
public Materia(){

}
//Constructor
public Materia(int id, String nome, int colore){
    this._id = id;
    this._nome = nome;
    this._colore = colore;

}
// constructor
public Materia(String nome, int colore){
    this._nome = nome;
    this._colore = colore;
}
// getting ID
public int getID(){
    return this._id;
}

// setting id
public void setID(int id){
    this._id = id;
}

//getting color
public int getColor(){

    return this._colore;

}
//setting color
public void setColor(int colore){

    this._colore = colore;

}
//getting nome materia
public String getMateria() {

    return this._nome;

}
//setting nome materia
 public void setMateria(String nome) {

     this._nome = nome;

 }
}

使用MySQLiteHelper类,我管理数据库:

public class MySQLiteHelper extends SQLiteOpenHelper {

//Database version
private static final int DATABASE_VERSION = 1;
//Database name
private static final String DATABASE_NAME = "materie.db";
//Materie table name
public static final String TABLE_MATERIE = "materie";
//Materie columns table names
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "nome";
public static final String COLUMN_COLOR = "colore";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_MATERIE_TABLE = "CREATE TABLE " + TABLE_MATERIE + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME + " TEXT,"
            + COLUMN_COLOR + " INTEGER," + ")";
    db.execSQL(CREATE_MATERIE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MATERIE);

    // Create tables again
    onCreate(db);
}

// Adding new contact
public void addMateria(Materia materia) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, materia.getMateria()); // Materia Name
    values.put(COLUMN_COLOR, materia.getColor()); // Materia color

    // Inserting Row
    db.insert(TABLE_MATERIE, null, values);
    db.close(); // Closing database connection
}

// Getting single contact
public Materia getMateria(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    //ELIMINATO COLUMN_DAY e COLUMN_HOUR
    Cursor cursor = db.query(TABLE_MATERIE, new String[] { COLUMN_ID,
                    COLUMN_NAME, COLUMN_COLOR }, COLUMN_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Materia materia = new Materia(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1),
            Integer.parseInt(cursor.getString(2)));
    // return contact
    return materia;
}

// Getting All Contacts
public List<Materia> getAllMaterie() {
    List<Materia> materiaList = new ArrayList<Materia>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_MATERIE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Materia materia = new Materia();
            materia.setID(Integer.parseInt(cursor.getString(0)));
            materia.setMateria(cursor.getString(1));
            materia.setColor(Integer.parseInt(cursor.getString(2)));
            // Adding contact to list
           materiaList.add(materia);
        } while (cursor.moveToNext());
    }

    // return contact list
    return materiaList;
}

// Getting contacts Count
public int getMateriaCount() {
    String countQuery = "SELECT  * FROM " + TABLE_MATERIE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

// Updating single contact
public int updateMateria(Materia materia) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, materia.getMateria());
    values.put(COLUMN_COLOR, materia.getColor());

    // updating row
    return db.update(TABLE_MATERIE, values, COLUMN_ID + " = ?",
            new String[] { String.valueOf(materia.getID()) });
}

// Deleting single contact
public void deleteMateria(Materia materia) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_MATERIE, COLUMN_ID + " = ?",
            new String[] { String.valueOf(materia.getID()) });
    db.close();
}

}//Close class database

正如您在方法public List<Materia> getAllMaterie()中所看到的,我从sqlite中获取所有材料并将它们放入列表中。

onCreate管理数据的活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_orario);

    MySQLiteHelper db = new MySQLiteHelper(this);

    //Get all materie inside database
    List<Materia> materia = db.getAllMaterie();
    for (Materia mat : materia) {
        //Change with loop all changed TextView into database by id
        TextView changedtextview = (TextView) findViewById(mat.getID());
        changedtextview.setText(mat.getMateria());
    }


}//Fine oncreate

在我的活动中,我需要收回输入数据库的所有材料,以更改用户触摸的TextView。 如何在List materia中获取单个id? 类似的东西:

 TextView changedtextview = (TextView)findViewById(materia._id);

但这不起作用。 有什么不对吗?

1 个答案:

答案 0 :(得分:0)

操作

List<Materia> materia = db.getAllMaterie();

您只需创建并填充对象集合。要显示此集合(列表),您应该将ListView与某些适配器(如ArrayAdapter或BaseAdapter)结合使用。适配器通常有一个方法getView(),负责用所需数据填充列表项(单元格)。而且我在代码中看不到任何ListView或adatpter。