Guest User

Untitled

a guest
Jun 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package alumnos.com.solemne2;
  2.  
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ListView;
  9. import android.widget.Toast;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. public class ListarActivity extends AppCompatActivity {
  15.  
  16. DBHelper dbhelper;
  17. ListView lv1;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_listar);
  23.  
  24. lv1 = (ListView) findViewById(R.id.lv);
  25. listar();
  26. }
  27.  
  28. public void listar() {
  29.  
  30. dbhelper = new DBHelper(this);
  31. SQLiteDatabase db = dbhelper.getReadableDatabase();
  32.  
  33. if (db != null) {
  34.  
  35. Cursor cursor = db.rawQuery("SELECT nombre FROM item;", null);
  36. List<String> items = new ArrayList<String>();
  37. if (cursor.moveToFirst()) {
  38. do {
  39. items.add(cursor.getString(cursor.getColumnIndex("nombre")));
  40. } while (cursor.moveToNext());
  41. }
  42.  
  43. String consulta = "SELECT * FROM datos;";
  44. Cursor c = db.rawQuery(consulta, null);
  45. int contador = c.getCount();
  46.  
  47. if (contador > 0) {
  48. String[] array = new String[contador];
  49. int i = 0;
  50. if (c.moveToFirst())
  51. {
  52. do {
  53.  
  54. String gasto = c.getString(0);
  55. String descripcion = c.getString(1);
  56. String fecha = c.getString(2);
  57. String item_id = c.getString(3);
  58. int nitemid = Integer.parseInt(item_id) + 1;
  59. String item_nombre = null;
  60.  
  61. String query_item = "SELECT item.nombre FROM item WHERE item.id_item = " + String.valueOf(nitemid) + ";";
  62. Cursor cursorItem = db.rawQuery(query_item, null);
  63. if (cursorItem.getCount() > 0) {
  64. if (cursorItem.moveToFirst()) {
  65. item_nombre = cursorItem.getString(0);
  66. }
  67. }
  68.  
  69.  
  70. String registro =
  71. "\nGasto " + (i+1) + "\n\nMonto: $" + c.getString(0) +
  72. "\nDescripcion: " + c.getString(1) +
  73. "\nFecha: " + c.getString(2) +
  74. "\nItem: " + item_nombre;
  75.  
  76. array[i] = registro;
  77. i++;
  78. } while (c.moveToNext());
  79. }
  80.  
  81.  
  82. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
  83. lv1.setAdapter(adapter);
  84. } else {
  85. Toast.makeText(this, "Error en la base de datos", Toast.LENGTH_SHORT).show();
  86. }
  87. }
  88.  
  89. }
  90. }
Add Comment
Please, Sign In to add comment