2

I am developing a basic android app which must handle several different things typically adding data and deleting an entry, must make use of an array.

at the moment I have a class that deals with adding a product this makes use of 2 edit texts fields and 4 spinners, when the user clicks on add product it will get the 2 textfields and 4 selected items from the spinner and add these all too the array.

deleting an item which just display all products held the user will then select the item they wish to delete and click the delete button.

I need some help with creating an array, will it be best to have a different class that deals with the array i.e creating the array when the app is ran and has methods for adding and deleting product.

I just want to know how it would possible to set this array up an array will need to hold the following:

product name (edit tect field)
category (edit text field)
price (spinner)
day (spinner)
month (spinner)
year (spinner)
0

2 Answers 2

2

How about this for a quick outline...

public class ProductActivity extends Activity {

private ArrayList<Product> _products = new ArrayList<Product>();

private EditText _nameEdtx;
// and other widgets

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

    // attach activity to layout and widgets in layout
}

// assuming your button has onClick="handleAddButtonClicked"
public void handleAddButtonClicked( View vw ) {
    _products.add( new Product( _nameEdtx.getText().toString() ) );
}

class Product {
    String _productName;
    // ... and so on
    public Product( String productName ) {
        _productName = productName;
        // and so on, with other member variables
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

One way is to create an object that has your items as attributes like so

import android.widget.EditText;
import android.widget.Spinner;

//declare class

public class MyClass
{
    public EditText ProductName;
    public EditText Category;
    public Spinner Price;
    public Spinner Day;
    public Spinner Month;
    public Spinner Year;
}

//now a list of them could look like
List<MyClass> myCollection = new ArrayList<MyClass>();

//iterating
for (MyClass item : myCollection)
{
    Log.d("DEBUG", "the current item ProductName is" + item.ProductName);
}

I do not know the full idea of your app so there maybe a legitimate reason for doing things this way however it does seem a little odd. I would have thought you create a class from primitive obects to represent a Product e.g.

public class Product
{
    public String ProductName;
    public String Category;
    public double Price;
    //etc
}

Then bound this to a list view (or something along these lines) and inflated a view for each row.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.