0

I'm stucked with filling an Array dynamic.

I can't find the solution. Normal I would fill the array just by any loop.

It's not working in this case I really spent hours to find a solution.

I found an example to use a custom list in android. works fine.

I create a object Test.

public class Test {
public int icon;
public int PB;
public String title;
public Test(){
    super();
}

public Test(int icon, String title, int PB) {
    super();
    this.icon = icon;
    this.title = title;
    this.PB = PB;
}

}

and fill it static here works fine. But I don't get how to fill it dynamic.

Test test_data[] = new Test[]
    {
        new Test(R.drawable.ic_launcher, "Test 1", 10),
        new Test(R.drawable.ic_launcher, "Test 2", 100)
    };
2
  • .... what dynamic means? Commented May 21, 2012 at 6:43
  • I'm getting data from a database and want to fill this object with the data everytime the activity starts. Commented May 21, 2012 at 6:50

3 Answers 3

1

dynamic assigning of data to an array as is follows:

 Test [] test_data = 
{
    new Test(R.drawable.ic_launcher, "Test 1", 10),
    new Test(R.drawable.ic_launcher, "Test 2", 100)
};

EDIT:

this can be done only in the first time you instance the array. if you already know how big the array is gonna be you should do this:

Test [] test_array = new Test[size];
for (int i=0; i<size; i++) {
    //DO STUFF
}

if you don;t know the size from the start what you should do is use a ListArray and then convert it to a simple array (or not), here is the code for that:

ListArray<Test> list = new ListArray<Test>();
// INSERT VALUES

public Test[] listToArray(ArrayList<Test> list) {
     Test [] result = new Test [list.size());
     for (int i = 0; i<list.size(); i++) {
        result[i] = list.get(i);
     }
     return result;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot! I knew theres a simple sultion i didn't get because my mind was stuck!
But well wait I have been too fast. Can you give me an example filling this by a loop? I'm really stucked :(
1

You can use ArrayList instead of static array.

    In case of array u have to define the size at compile time and then add objects to it like:

    Test test_data[] = new Test[10];
    test_data[0] = new Test(R.drawable.ic_launcher, "Test 1", 10);
    test_data[1] = new Test(R.drawable.ic_launcher, "Test 2", 100);
    ....................

I tried the code above. This is what giving me the NULL Pointer Exception.

I'm getting data from a database and want to fill this object with the data everytime the activity starts


 You have to use ArrayList like:

 ArrayList<Test> test_data = new ArrayList<Test>;

 test_data.add(new Test(R.drawable.ic_launcher, "Test 1", 10));
 test_data.add(new Test(R.drawable.ic_launcher, "Test 2", 100));

..........
And clear ArrayList by test_data.clear()

4 Comments

I tried the first Code Example he gave me. If I use an ArrayList I have to restructe the whole code.
I took the first code example and i got an the exception. If I change to an ArrayList i have to change a lot. So I'm looking for a way to avoid this.
Tried it over and over now its working due any magic reason! Thanks alot.
@HimanshuMohta, you have some errors in your code. since it has already been edited, I can't fix them, but you should notice that ArrayList should call a constructor (new ArrayList<Test>())
0

You need to initialize the array. Since you are using a database, the data must be coming from a cursor, so you can do this:

Test test_data[] = new Test[cursor.getCount()] 

That will create a slot for each record in your cursor. Then you can run through your loop and populate it.

1 Comment

The Size isnt the Problem. I tried this static It's giving me a NULL Pointer Exception.

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.