使用SQLite项填充ListView

时间:2016-02-22 00:07:08

标签: android sqlite listview android-sqlite

好的,我是Android的新手,我正在努力打造一个定制的家庭作业经理 在min活动(ListaTareas)中,我调用了适配器,之前我有一些statick示例HW,但现在我希望它从数据库中读取它们。
我的数据库管理器上有一个方法,它将获取表中的所有元素并将它们放在List中,但现在我如何将它们放在ListView中?

这是我的一些代码

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;
}

2 个答案:

答案 0 :(得分:0)

不仅仅是向构造函数发送上下文,还要发送元素。在ListView中显示元素是数据库的另一个主题。您应该了解ArrayAdapter和ListView的工作原理。
为了好的做法,请查看以下地址。 http://www.vogella.com/tutorials/AndroidListView/article.html

@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,getTereas());//add 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<Tarea>{

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;
List<Tarea> objects;
Adaptador(Activity context,List<Tarea> objects)
{
    super(context, R.layout.elemento_lista, objects);
    this.context = context;
    this.objects = objects;
}

//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(objects.get(position).getTitulo());

    TextView lblsubtitulo = (TextView) item.findViewById(R.id.lblSubtitulo);
    lblsubtitulo.setText(objects.get(position).getSubtitulo());

    ImageView lblImagen = (ImageView) item.findViewById(R.id.lblImagen);
    lblImagen.setImageResource(objects.get(position).getImagen());

    return (item);
}

答案 1 :(得分:0)

public List<Tarea> getTareas() {
List<Tarea> listaTarea = new ArrayList<Tarea>();
// Select All Query
String selectQuery = "SELECT  * FROM " + TABLA_TAREAS;
   //change this line  
SQLiteDatabase db = this.getWritableDatabase();
// Change to
  SQLiteDatabase db = this.getReadableDatabase();    
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;

}

use getReadableDatabase();
while reading data and
getWritableDatabase(); while writing to database

希望这有助于谢谢你。 有关使用数据库的信息,请参阅: - https://androidruler.wordpress.com/2016/02/22/android-working-with-sqlite-database/