The data is not shown in the listview with SQLite (Android studio)

1

I am creating an application in android studio, on a menu of a cafeteria, what I want to do is that when the person clicks on the "hamburger bbq" button (to give an example) a dialogue box is displayed that ask if you really want to add the hamburger to the order, it is assumed that when you click on "YES" a new activity called "Orders" is opened and in this activity a listview shows what you are ordering (name of the food and the cost of the food), my problem is that I'm doing it with SQLite (based on several youtube tutorials) and when the "Orders" activity opens, nothing is shown in the list and the code does not Mark any error.

Before I put the SQLite if it showed, but it was erased every time I changed the screen or when I added something else to the order.

MyDBHandler.java

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class MyDBHandler extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "comida.db";
public static final String TABLE_PRODUCTS = "comidas";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COSTO = "costocomida";
public static final String COLUMN_NOMBRECOMIDA = "comida";

public MyDBHandler(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(" CREATE TABLE " + TABLE_PRODUCTS + "(" +
    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_NOMBRECOMIDA + " TEXT, " +
            COLUMN_COSTO + " TEXT" +
            ")");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
    onCreate(db);
}
public void close(){
    this.close();
}

public void addComida(String comida, String costocomida) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_NOMBRECOMIDA, comida);
    values.put(COLUMN_COSTO, costocomida);
    this.getWritableDatabase().insert(TABLE_PRODUCTS, null, values);
}

public Cursor getComida() {
    String[] columnas = {COLUMN_ID, COLUMN_NOMBRECOMIDA, COLUMN_COSTO};
    Cursor c = this.getReadableDatabase().query(TABLE_PRODUCTS, columnas, null, null, null, null, null);
    return c;
}
}

Hamburguesas.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Hamburguesas extends AppCompatActivity {
Button bbq;
int costohamburguesa = 0;
MyDBHandler dbHandler;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_hamburguesas);
    bbq = (Button) findViewById(R.id.bbq);

    dbHandler = new MyDBHandler(this);

        bbq.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder mBuilder = new AlertDialog.Builder(Hamburguesas.this);
            mBuilder.setTitle("AGREGAR");
            mBuilder.setMessage("¿Desea agregar a su orden?");
            mBuilder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {


                    dialogInterface.dismiss();
                    costohamburguesa = 40;
                    String costohamburguesabbq = Integer.toString(costohamburguesa);
                    String nombre = bbq.getText().toString();
                    dbHandler.addComida(nombre,costohamburguesabbq);

                    Intent intent = new Intent(Hamburguesas.this,Pedidos.class);
                    startActivity(intent);


                }
            });
            mBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            AlertDialog alertDialog = mBuilder.create();
            alertDialog.show();            }
    });

Orders.java

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;


public class Pedidos extends AppCompatActivity {
private ListView list;
private ArrayList<String> arrayList;
Button regresarmenu;
int costocomida;
String comida;
MyDBHandler dbHandler;
Button mostrar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pedidos);
    list = (ListView) findViewById(R.id.list);
    mostrar = (Button) findViewById(R.id.mostrar);

    showNotes();

        //botón para regresar al menú
        regresarmenu = findViewById(R.id.regresarmenu);
        regresarmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Pedidos.this, Menu.class);
                startActivity(intent);

            }
        });

    }
private void showNotes() {
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        dbHandler = new MyDBHandler(this);
        Cursor c = dbHandler.getComida();
        arrayList = new ArrayList<String>();
        String title;
        String content;
        if (c.moveToFirst()) {
            do {
                title = c.getString(1);
                content = c.getString(2);
                arrayList.add(title + "         $" + content);
            } while (c.moveToNext());
        }
        ArrayAdapter<String> adapters = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, arrayList);
        list.setAdapter(adapters);
        adapters.notifyDataSetChanged();

    }

}
}
    
asked by Guillermo Cézar Gutiérrez 31.03.2018 в 00:18
source

0 answers