A
A
Ainur Shakirov2016-03-27 09:40:15
Java
Ainur Shakirov, 2016-03-27 09:40:15

Dynamic size of an array of java objects?

android.leocardz.com/multiple-layout-listview
Made according to the lesson, everything is fine.
But how to define a dynamic array? I don't know beforehand how many elements there will be.
Tried it in different ways, but I'm still a noob in Java.
PS Yes, I know that RecyclerView is better, but ListView is also suitable for my purposes.
My goal is simple: to display data from the table in a more or less beautiful form. I write for myself. Along the way, getting acquainted with ANDROID DEVELOPMENT.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ainur Shakirov, 2016-03-27
@Fqyeh29

Solution (if anyone needs it):

//Создаю:
List<List<String>> list = new ArrayList<>();
//Добавляю в цикле так:
list.add(new ArrayList());
list.get(list.size()-1).add(text);
list.get(list.size()-1).add(text2+"");

//Затем(после работы цикла) заполняю:
final ListViewItem[] items = new ListViewItem[list.size()];
li=0;
for (List item : list) {
items[li] = new ListViewItem(item.get(0),Integer.parseInt(item.get(1)));
li++;
}

Not the best, but simple and working.

A
Alexander Vasilenko, 2016-03-27
@SanchelliosProg

Dude, use collections. I think ArrayList can suit you well for this task. And also, learn Java well, then it will be easier to breathe in Android development.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question