2015-03-30 33 views
0

我有一个sqlite数据库包含一些TextViews的信息,如文本,背景颜色和更改后的TextView的id,因为我有一个TableView与60个TextViews。 当用户触摸其中的一个时,他可以更改TextView的内容和背景颜色。 我的问题是,当我收回所有保存的TextView时,我将它们放入列表中。Android如何从Sqlite查询加载n TextViews与数据

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 S中的从用户触摸的所有本草。 我该如何在List materia中使用单个ID? 类似于:

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

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

回答

0

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

您只需创建并填写您的对象的集合。为了显示这个集合(列表),你应该使用一个ListView和一些像ArrayAdapter或BaseAdapter这样的适配器。适配器通常有一个方法getView(),它负责填充所需数据的列表项(单元格)。而且我在代码中看不到任何ListView或Adatpter。

+0

好的,我不确定我是否理解你。 我更改了从存储到数据库中的ID触摸的TextView。 但我不知道如何进入对象材料的单个字段。 列表 materia是一个对象列表,对不对? 那么,我可以访问单个字段吗?像'id = materia.id' 或者为了做到这一点,我需要你在你的答案上写的getView()? 你可以发表一些代码,所以我可以理解你的意思吗? – Mario 2015-03-30 21:43:15

+0

如果你有一个Materie对象的列表,所以这个集合中有一个你正在寻找的对象,对吧?因此,只需找到您需要的对象,并从中获得所需的所有信息。迭代集合以查找您需要的目标对象。当你发现目标对象时,你可以像你提到的那样访问它的数据,比如id = materia.id。 – Stan 2015-03-30 21:50:12

+0

啊,你不明白'TextView'与你存储在sql db中的文本完全没有关系。你不能存储VIEW事物,但你只能存储一些你可以从'View'获得的数据。就像使用'textView.getText()''TextView'的'String'或者textview文本的颜色一样。你不能存储视觉部分。 Materie中没有任何TextView。 TextView是布局的一部分。然后你编写'setContentView(R.layout.activity_set_orario);'这意味着布局文件'activity_set_orario.xml'可以包含一些'TextViews',所以你可以使用'findViewById()'找到'TextView'。 – Stan 2015-03-30 21:59:00