Jump to content

Need help for doing something with Java

Go to solution Solved by Stardar1,
Just now, Cryosec said:

that's something I never attempted to do. What would be the file layout and type for something like this?

Probably a .txt, as for the format insie it, that is for you to determine, as you are reading it. 

I'm back here to ask something else for an android app I'm coding

 

In the main activity, when user presses the floating action button, a second activity is launched. Here the users adds stuff to a list (name, price, quantity), which then gets saved to an SQLite table.

What I need to do is: for every time the user presses the FAB in the MainActivity, a new table must be created (with unique table name), and a new entry in the listview of mainactivity, related to the newly created table, should appear (and also be saved).

I can't find a way to make this possible, any ideas?

 

(I already have a DBHelper class)

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to comment
https://linustechtips.com/topic/591267-need-help-for-doing-something-with-java/
Share on other sites

Link to post
Share on other sites

3 minutes ago, Cryosec said:

I'm back here to ask something else for an android app I'm coding

 

In the main activity, when user presses the floating action button, a second activity is launched. Here the users adds stuff to a list (name, price, quantity), which then gets saved to an SQLite table.

What I need to do is: for every time the user presses the FAB in the MainActivity, a new table must be created (with unique table name), and a new entry in the listview of mainactivity, related to the newly created table, should appear (and also be saved).

I can't find a way to make this possible, any ideas?

 

(I already have a DBHelper class)

Well you could make a list of tables. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

2 minutes ago, Stardar1 said:

Well you could make a list of tables. 

aaand how could I tell the DBHelper to use a unique table name each time the button is pressed? also, what would be the variable type for a table (assuming you intend List<item>)?

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

3 minutes ago, Cryosec said:

aaand how could I tell the DBHelper to use a unique table name each time the button is pressed? also, what would be the variable type for a table (assuming you intend List<item>)?

Rather than a unique name you would have an index in the list, as Java cannot create and store variable with unique names while running the code. 

 

as for the tables, and their types, that should be easy to figure out. (I have not used them)

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

3 minutes ago, Stardar1 said:

Rather than a unique name you would have an index in the list, as Java cannot create and store variable with unique names while running the code. 

 

but I wouldn't have different tables, as they all have the same name. SQLite would create the first table, and when I need to create the next table, at index 1, it would find a table in the database with the same name, so it won't create a new one.

 

But if you mean instead of a TABLE_NAME I used the index in the list, then I'd have to change the onCreate and onUpdate functions in the DBHelper, which would give me an error (onCreate and onUpdate won't be called anymore, as they won't be the default function the class expects)

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

1 minute ago, Cryosec said:

but I wouldn't have different tables, as they all have the same name. SQLite would create the first table, and when I need to create the next table, at index 1, it would find a table in the database with the same name, so it won't create a new one.

 

But if you mean instead of a TABLE_NAME I used the index in the list, then I'd have to change the onCreate and onUpdate functions in the DBHelper, which would give me an error (onCreate and onUpdate won't be called anymore, as they won't be the default function the class expects)

Can you not just create a new table object in the list?

 

 

list.add(new Table(<args));

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

2 minutes ago, Stardar1 said:

Can you not just create a new table object in the list?

tables are created with SQL queries within the DBHelper class:

 

Spoiler

private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "shopList";
    private static final String TABLE_NAME = "shopTable";
    // Table column names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "Name";
    private static final String KEY_PRICE = "Price";
    private static final String KEY_QTY = "Quantity";

--snip--

public void onCreate(SQLiteDatabase db) {

        String CREATE_SHOP_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PRICE + " INTEGER," + KEY_QTY + " INTEGER" + ")";
        db.execSQL(CREATE_SHOP_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

 

 

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

Just now, Cryosec said:

tables are created with SQL queries within the DBHelper class:

 

  Reveal hidden contents


private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "shopList";
    private static final String TABLE_NAME = "shopTable";
    // Table column names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "Name";
    private static final String KEY_PRICE = "Price";
    private static final String KEY_QTY = "Quantity";

--snip--

public void onCreate(SQLiteDatabase db) {

        String CREATE_SHOP_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PRICE + " INTEGER," + KEY_QTY + " INTEGER" + ")";
        db.execSQL(CREATE_SHOP_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

 

 

Then make a class that creates a table. Store those in the list. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

10 minutes ago, Stardar1 said:

Then make a class that creates a table. Store those in the list. 

A class that creates a table would be a copy of DBHelper, thus still keeping the same name for each table created

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

Just now, Cryosec said:

A class that creates a table would be a copy of DBHelper, thus still keeping the same name for each table created

But that doesnt matter because you would be creating a new instance every time in the list. 

 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

3 minutes ago, Stardar1 said:

But that doesnt matter because you would be creating a new instance every time in the list. 

It would matter, as the database would find a table with the same name with the query "CREATE TABLE IF NOT EXISTS", preventing the creation of the new table

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

Just now, Cryosec said:

It would matter, as the database would find a table with the same name with the query "CREATE TABLE IF NOT EXISTS", preventing the creation of the new table

Java cannot create variables with your custom names once it has been compiled, not unless the table has a field that takes in a string for it's name. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

2 minutes ago, Stardar1 said:

Java cannot create variables with your custom names once it has been compiled, not unless the table has a field that takes in a string for it's name. 

theeeen the database idea won't work.

I'll need another way to save entries in a ListView, and also be able to load a specific saved list from an entry in another listview of another activity

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

Just now, Cryosec said:

theeeen the database idea won't work.

I'll need another way to save entries in a ListView, and also be able to load a specific saved list from an entry in another listview of another activity

You can use a file...

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

2 minutes ago, Stardar1 said:

You can use a file...

that's something I never attempted to do. What would be the file layout and type for something like this?

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

Just now, Cryosec said:

that's something I never attempted to do. What would be the file layout and type for something like this?

Probably a .txt, as for the format insie it, that is for you to determine, as you are reading it. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

2 minutes ago, Stardar1 said:

Probably a .txt, as for the format insie it, that is for you to determine, as you are reading it. 

So if I got this correctly, I should create a txt file when a new list is created (the FAB button in the mainactivity), and for every new entry, I'll save it also to the file.

Something like this (the txt):

name,price,qty;
name,price,qty;
name,price,qty;

Then, to read the file and re-create the list, I can make it read until the comma, use the input as "name", read to the next comma, and so on. Sounds good, what do you think?

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

1 minute ago, Cryosec said:

So if I got this correctly, I should create a txt file when a new list is created (the FAB button in the mainactivity), and for every new entry, I'll save it also to the file.

Something like this (the txt):


name,price,qty;
name,price,qty;
name,price,qty;

Then, to read the file and re-create the list, I can make it read until the comma, use the input as "name", read to the next comma, and so on. Sounds good, what do you think?

Will work great. Is the user inputing those parameters?

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

1 minute ago, Stardar1 said:

Will work great. Is the user inputing those parameters?

In the second activity, user will be prompted (after pressing another FAB) with a popup. This has 3 EditTexts where the user will input the name, the price and the quantity (stored as String name, int price, int qty. obviouslty I can convert price and qty to string if needed)

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

4 minutes ago, Cryosec said:

In the second activity, user will be prompted (after pressing another FAB) with a popup. This has 3 EditTexts where the user will input the name, the price and the quantity (stored as String name, int price, int qty. obviouslty I can convert price and qty to string if needed)

ok. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to post
Share on other sites

2 minutes ago, Stardar1 said:

ok. 

Well, thanks for helping me :D 

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

You would probably want to use a double or BigInt for pricing to allow for decimals (use Double.toString(doublevalue); to make it a String if you want to). That gives you the option to have prices like "$5.99" that you can perform Math.function() on later.

 

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

3 minutes ago, Fetzie said:

You would probably want to use a double or BigInt for pricing to allow for decimals (use Double.toString(doublevalue); to make it a String if you want to). That gives you the option to have prices like "$5.99" that you can perform Math.function() on later.

 

that actually helps, I was going to do some research on what should be the best type. Thanks!

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×