3

In my project i have a array list like this :

{"data": 
[
{"Title": "Fiat 500", "Url":"http://lh5.ggpht.com/-wgCH-P1bWCo/UWW_eCUXsVI/AAAAAAAMaKI/MlRF0GzwF8Y/s1200/Fiat-Abarth-500-US-6%25255B2%25255D.jpg"},
{"Title": "Alfa Romeo", "Url": "http://i.auto-bild.de/ir_img/1/2/4/5/4/9/7/Alfa-Romeo-4C-Spider-Vorstellung-und-Preis-1200x800-24a91d418ddbd1fa.jpg"},
{"Title": "500L", "Url":"http://cdn1.autoexpress.co.uk/sites/autoexpressuk/files/fiat-500-turbo-front-action.jpg"},
{"Title": "Egea", "Url":"https://pbs.twimg.com/media/C6vp_VcU4AAaM0t.jpg"},
{"Title": "Maserati", "Url":"http://lh4.ggpht.com/-CjXVq7s2roU/U06cyjInzjI/AAAAAAAQwvI/xuJUZ0BbOUw/s1200/Maserati-Centennial-1%25255B2%25255D.jpg"},
{"Title": "Lancia", "Url":"http://lh6.ggpht.com/_FoXyvaPSnVk/TWl73Gr0lmI/AAAAAAAECw0/ZK7nAKZ6mdY/s1200/Lancia-Chrysler-30%5B5%5D.jpg"},
{"Title": "Jeep", "Url":"http://lh3.ggpht.com/-1UvJYZn4fR0/UyGvPSDIrZI/AAAAAAAQdR0/s2c70tIu1i0/s1200/Jeep-2%25255B3%25255D.jpg"},
{"Title": "Ferrari", "Url":"http://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2016/02/05/103366052-1957s.jpg?v=1454698344"},
{"Title": "Alfa Romeo", "Url":"http://lh3.ggpht.com/-lYmpe0bGJko/UnqlBB040wI/AAAAAAAO4yE/HbUaH9RxWR8/s1200/Alfa-Romeo-Gloria-Concept-by-IED-1%25255B4%25255D.jpg"},
{"Title": "Maserati", "Url":"http://zonderpump.com/images/maserati-granturismo-sport-can-you-feel-it2.jpg"}
]
}

and i want to add items like this however viewpager and adapter get one item only. So i want to make my ArrayList dimentional and set text like this : textview.setText(list.getUsername[0 or 1]);

how can i do this. Thanks much. By the way i found this code :

function listToMatrix(list, elementsPerSubArray) {
    var matrix = [], i, k;

    for (i = 0, k = -1; i < list.length; i++) {
        if (i % elementsPerSubArray === 0) {
            k++;
            matrix[k] = [];
        }

        matrix[k].push(list[i]);
    }

    return matrix;
}

var matrix = listToMatrix([1, 2, 3, 4, 4, 5, 6, 7, 8, 9], 3);
// result: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

but how can i make this arraylist items.?

0

1 Answer 1

5

It looks like your data is in JSON format; then you should rather look into existing technologies; not only for parsing; but also for "how to model my data"; see here as starting point.

But to answer your actual question - let's look into different options.

First, your own suggestion a two-dim list. That would look like:

List<List<String>> matrix = new ArrayList<>();
// and then for each row (giving you code that creates similar data)
List<String> row = new ArrayList<>();
row.add("Title");
row.add("Fiat 500"); ...

matrix.add(row);

The idea: you create such row lists for each input line; and then you add those rows to your matrix list.

But when you have a closer look you find that the first and third column are a "key" for the second/forth column. So a better fit could be:

List<Map<String, String>> matrix = new ArrayList<>();
// and then for each row (giving you code that creates similar data)
Map<String, String> row = new HashMap<>();
row.put("Title", "Fiat 500"); ...
matrix.add(row);

Meaning: you use a data structure that preserves that mapping between first and second column for example.

Finally, so far we were using "flat" data structures. But we could instead build a special class, like:

public class CarPictureInfo {
  private final String title;
  private final URL url;

  public CarPictureInfo(String title, URL url) ...

And now you create a List<CarPictureInfo> objects; and use the data of one row to create one CarPictureInfo object. (please note: I suggested here to not store that URL as "raw" String, but to convert it into a type that has more meaning than well, a raw string).

These options have different advantages and disadvantages; and in the end it depends on what you want to do with your data to understand which solution best fits your needs.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply. It's help me lot.

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.