lundi 30 mars 2015

Android how load n TextViews with data from Sqlite query

I have an sqlite db that contain information of some TextViews, like text, background color and id of the changed TextView, because i have a TableView with 60 TextViews. When the user touch one of them, he can change the content of the TextView and the background color. My problem is that when i take back all the saved TextView i put them into a list.


Materia.java is my object



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;

}
}


With the class MySQLiteHelper i manage the database



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


As you can see with the method public List<Materia> getAllMaterie() i take all materie from sqlite and put them into a list.


onCreate of the activity that manage data:



@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();

//Cambio ciclicamente le textview presenti nel database
TextView changedtextview = (TextView)findViewById(materia.);
changedtextview.setText(materia.nome);


}//Fine oncreate


In my Activity i need to take back all the materie inputed into database for change the TextViews that are touched from user. How can i take the single id's in the List materia? Something like:



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


But this not work. There is something wrong?


Aucun commentaire:

Enregistrer un commentaire