0

My goal is simple. I'd like to be able to take user input from 4 edittext fields, with ids of et1-4, and put them into an array named aSummary1[] = {et1, et2, et3, et4};. The four fields are different data types, two being words, one numbers, and one a password. This will all be displayed in a ListView. This part I can do. The part I can't figure out is if I want to add a new row in the ListView, I'd like to create a new string array, let's say aSummary2[] with different values, but the same parameters: {word, word, number, password};, then aSummary3[] and so on and so forth.

How would one go about doing this? Or if there is a better way, how would I assign variables and strings to list entries with no limit. For instance, with list entry x comes variables w y and z assigned to x, but with variable a comes b c and e, and with q comes r s and t.

1
  • 1
    Why Array? Why not object of a class? Commented Sep 2, 2012 at 12:14

2 Answers 2

2

I think you need a growing array..... Better go with Collections, its more flexible, and unlike arrays, you don't initialize it at the time of its declaration.

First Consider making a Pojo

public class Summary {

    private String str1;
    private String str2;
    private int number;
    private String password;



    public Summary(String str1, String str2, int number, String password) {

        this.str1 = str1;
        this.str2 = str2;
        this.number = number;
        this.password = password;
    }

    public String getStr1() {
        return str1;
    }

    public String getStr2() {
        return str2;
    }

    public int getNumber() {
        return number;
    }

    public String getPassword() {
        return password;
    }


}



 }

Now consider another class from where you initialize this class object and store then in an ArrayList.

public class Test{

public static ArrayList<Summary> arList = new ArrayList<Summary>();
private Summary sObj;

   public void fillMe(String p1, String p2, int p3, String p4){


           arList.add(new sObj(p1, p2, p3, p4));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

As was mentioned in the comments, the better solution will be to create a class containing all required fields, for example:

public class Summary {
    private String field1;
    private String field2;
    private String field3;
    private String field4;
}

And then store objects of this class inside an ArrayList:

ArrayList<Summary> summaries = new ArrayList<Summary>();

When you need to create another object of this type, just call:

Summary summary = new Summary();
// initialize fields
summaries.add(summary);

Hope this helps.

4 Comments

When I want to call a specific variable from a specific location, eg. aSummary1[0] or aSummary14[3], how would I do that?
@Adam, Just call summaries.get(position)
The position would increase by 4 every time. The password would be at summaries.get(3) for the first password, but the second would be summaries.get(7). The twelfth password would be at summaries.get(47).
@Adam, You're not right. Password should be just a field of a Summary object, means to get first password you should get first item of summaries and retrieve its password.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.