Ok, I am new to Android and I am trying to make a custom homework manager. From the min activity (ListaTareas) I call the Adapter, before I had and array of statick example HWs but now I would like it to read them from the database. I have a method on my database manager that will take all the elements in the table and put them in a List, but now how do I put them in the ListView??
Thanks
here is some of my code
public class ListaTareas extends Activity {
ImageButton FAB;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// para quitar titulo
setContentView(lista_tareas);
Adaptador adaptador = new Adaptador(this);
ListView lstOpciones = (ListView) findViewById(R.id.listaTareas);
lstOpciones.setAdapter(adaptador);
FAB = (ImageButton) findViewById(R.id.fab);
FAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(ListaTareas.this, CrearTarea.class);
startActivity(i);
}
});
}
}
public class Adaptador extends ArrayAdapter<Object>{
BaseDeDatos db;
private static Tarea[] datos = new Tarea[]{
new Tarea("Tarea 1",
"Descripcion tarea",
"24/02/2016"
//,R.drawable.hw
),
new Tarea("Tarea 2",
"Descripcion tarea",
"24/02/2016"
//,R.drawable.hw
)
};
//Constructor de la clase Adaptador
Activity context;
Adaptador(Activity context)
{
super(context, R.layout.elemento_lista, datos);
this.context = context;
}
//Metodo para asignar cada elemento del list item a los datos
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View item = inflater.inflate(R.layout.elemento_lista, null);
TextView lbltitulo = (TextView) item.findViewById(R.id.lblTitulo);
lbltitulo.setText(datos[position].getTitulo());
TextView lblsubtitulo = (TextView) item.findViewById(R.id.lblSubtitulo);
lblsubtitulo.setText(datos[position].getSubtitulo());
ImageView lblImagen = (ImageView) item.findViewById(R.id.lblImagen);
lblImagen.setImageResource(datos[position].getImagen());
return (item);
}
}
public List<Tarea> getTareas() {
List<Tarea> listaTarea = new ArrayList<Tarea>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLA_TAREAS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Tarea tarea = new Tarea();
tarea.setTitulo(cursor.getString(1));
tarea.setSubtitulo(cursor.getString(2));
tarea.setFecha(cursor.getString(3));
// Adding contact to list
listaTarea.add(tarea);
} while (cursor.moveToNext());
}
// return contact list
return listaTarea;
}
Aucun commentaire:
Enregistrer un commentaire