Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package alumnos.com.solemne2;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- public class ListarActivity extends AppCompatActivity {
- DBHelper dbhelper;
- ListView lv1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_listar);
- lv1 = (ListView) findViewById(R.id.lv);
- listar();
- }
- public void listar() {
- dbhelper = new DBHelper(this);
- SQLiteDatabase db = dbhelper.getReadableDatabase();
- if (db != null) {
- Cursor cursor = db.rawQuery("SELECT nombre FROM item;", null);
- List<String> items = new ArrayList<String>();
- if (cursor.moveToFirst()) {
- do {
- items.add(cursor.getString(cursor.getColumnIndex("nombre")));
- } while (cursor.moveToNext());
- }
- String consulta = "SELECT * FROM datos;";
- Cursor c = db.rawQuery(consulta, null);
- int contador = c.getCount();
- if (contador > 0) {
- String[] array = new String[contador];
- int i = 0;
- if (c.moveToFirst())
- {
- do {
- String gasto = c.getString(0);
- String descripcion = c.getString(1);
- String fecha = c.getString(2);
- String item_id = c.getString(3);
- int nitemid = Integer.parseInt(item_id) + 1;
- String item_nombre = null;
- String query_item = "SELECT item.nombre FROM item WHERE item.id_item = " + String.valueOf(nitemid) + ";";
- Cursor cursorItem = db.rawQuery(query_item, null);
- if (cursorItem.getCount() > 0) {
- if (cursorItem.moveToFirst()) {
- item_nombre = cursorItem.getString(0);
- }
- }
- String registro =
- "\nGasto " + (i+1) + "\n\nMonto: $" + c.getString(0) +
- "\nDescripcion: " + c.getString(1) +
- "\nFecha: " + c.getString(2) +
- "\nItem: " + item_nombre;
- array[i] = registro;
- i++;
- } while (c.moveToNext());
- }
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
- lv1.setAdapter(adapter);
- } else {
- Toast.makeText(this, "Error en la base de datos", Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment