Reputation: 45
I am new in android studio, I am working with listItem. below is some of my code
String[] items = {
"1 st",
"2 nd",
"3 rd",
"4 th"};
titleList = new ArrayList<>(Arrays.asList(items));
adapter = new ArrayAdapter<String>(this,R.layout.list_item, R.id.txtitem,titleList);
listView.setAdapter(adapter);
Above code working fine. but i want work with more then 1 Array tree I want to work with 2D array but i can't understand what to do here is my new code I tried. but failed
String[][] items = {
{"1 st","12 th Sept"},
{"2 nd","13 th Sept"},
{"3 rd","14 th Sept"},
{"4 th","15 th Sept"},
};
titleList = new ArrayList<>(Arrays.asList(items[0]));
dateList = new ArrayList<>(Arrays.asList(items[1]));
adapter = new ArrayAdapter<String>(this,R.layout.list_item, R.id.txtitem,titleList);
adapter = new ArrayAdapter<String>(this,R.layout.list_item, R.id.date,dateList);
listView.setAdapter(adapter);
Upvotes: 2
Views: 9545
Reputation: 66516
A 2-dimensional is an array that contains arrays as elements. Those elements or arrays can both have same length or same number of int values or varying numbers which can break matrix approach.
int[][] myArray= new array[3][2];
means this is an array that contains 3 arrays with 2 int values(values are 0 by default).
myArray[0] = {0,0};
myArray[1] = {0,0};
myArray[2] = {0,0};
Or you can interpret this as 3x2 matrix with 3 rows and 2 columns You can also declare 2 dimensional arrays that contains 1 dimensional arrays with varying sizes too.
int[][] otherArray= new array[3][];
otherArray[0] = new array[3];
otherArray[1] = new array[4];
otherArray[2] = new array[5];
Or you can assign initial values to elements.
otherArray[0] = {1,2,3};
otherArray[1] = {4,5,6,7};
otherArray[2] = {1,2,3,4,5};
Now you can get otherArray[2][4] which returns 5 but you can't get otherArray[1][4] since it does not exist and returns IndexOutOfBoundException. 2 dimensional arrays are arrays that contain arrays.
Upvotes: 0
Reputation: 1135
1) You should change the order of your 2d array
like below. But it will still not work (See next point)
String[][] items = {
{"1 st", "2 nd", "3 rd", "4 th"},
{"12 th Sept", "13 th Sept", "14 th Sept", "15 th Sept"},
};
2) Below, in the first line, your adapter
is refering to the ArrayAdapter
with titlelist
and in next line, you are changing the same adapter object
to point to datelist
ArrayAdapter
.
adapter = new ArrayAdapter<String>(this,R.layout.list_item, R.id.txtitem,titleList);
adapter = new ArrayAdapter<String>(this,R.layout.list_item, R.id.date,dateList);
So, after 2nd line, your adapter is no longer pointing to titlelist
ArrayAdapter. It is referring to datelist
ArrayAdapter only. Actually, it can never hold both datelist
and titlelist
together.
What's the right way then?
Create a model class
containing title
and date
like below -
class YourModel {
String title;
String date;
}
Create an array
of objects of this model class and pass this array of objects to your custom adapter
. It's the only way. You will have to create custom ArrayAdapter. Learn about it from here.
Upvotes: 1
Reputation: 1527
I think that array instantiation is the correct syntax, but you're creating a 4 x 2 array, when it looks like you want a 2x4 array.
Have you tried:
String[][] items = {
{"1 st", "2 nd", "3 rd", "4 th"},
{"12 th Sept", "13 th Sept", "14 th Sept", "15 th Sept"},
};
Upvotes: 0