【问题标题】:Working with 2D array in android studio在 android studio 中使用二维数组
【发布时间】:2018-03-03 06:55:23
【问题描述】:

我是安卓工作室的新手, 我正在使用 listItem。下面是我的一些代码

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);

以上代码工作正常。但我想使用超过 1 个数组树我想使用 2D 数组但我不明白该怎么做是我尝试过的新代码。但失败了

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);

【问题讨论】:

标签: java android arrays listview android-studio


【解决方案1】:

二维是包含数组作为元素的数组。这些元素或数组都可以具有相同的长度或相同数量的 int 值或不同的数量,这可能会破坏矩阵方法。

int[][] myArray= new array[3][2]; 表示这是一个包含 3 个具有 2 个 int 值的数组的数组(默认值为 0)。

myArray[0] = {0,0};
myArray[1] = {0,0};
myArray[2] = {0,0};

或者您可以将其解释为 3 行 2 列的 3x2 矩阵 您也可以声明包含不同大小的一维数组的二维数组。

int[][] otherArray= new array[3][];
otherArray[0] = new array[3];
otherArray[1] = new array[4];
otherArray[2] = new array[5];

或者您可以为元素分配初始值。

otherArray[0] = {1,2,3};
otherArray[1] = {4,5,6,7};
otherArray[2] = {1,2,3,4,5};

现在您可以获得返回 5 的 otherArray[2][4],但您无法获得 otherArray[1][4],因为它不存在并返回 IndexOutOfBoundException。二维数组是包含数组的数组。

【讨论】:

    【解决方案2】:

    1) 你应该改变2d array 的顺序,如下所示。但它仍然不起作用(见下一点)

    String[][] items = {
             {"1 st", "2 nd", "3 rd", "4 th"},
    {"12 th Sept", "13 th Sept", "14 th Sept", "15 th Sept"},
        };
    

    2) 下面,在第一行,您的adaptertitlelist 引用ArrayAdapter,在下一行,您正在更改相同 adapter object 指向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);
    

    因此,在第 2 行之后,您的适配器不再指向 titlelist ArrayAdapter。它仅指datelist ArrayAdapter。实际上,它永远无法同时容纳datelisttitlelist

    那么正确的方法是什么?

    创建一个包含titledate 的模型class,如下所示 -

    class YourModel {
    String title;
    String date;
    }
    

    创建此模型类的对象array,并将此对象数组传递给您的自定义adapter。这是唯一的方式。您将必须创建自定义 ArrayAdapter。通过here了解它。

    【讨论】:

      【解决方案3】:

      我认为数组实例化是正确的语法,但是您正在创建一个 4 x 2 数组,而看起来您想要一个 2x4 数组。

      你试过了吗:

      String[][] items = {
               {"1 st", "2 nd", "3 rd", "4 th"},
               {"12 th Sept", "13 th Sept", "14 th Sept", "15 th Sept"},
      };
      

      【讨论】:

      • @SUJOYROY 就像我在回答中所说的那样,它不起作用。您必须创建自定义 ArrayAdapter。
      • @SachinAggarwal 我在您的回答中访问了您建议的页面。但我不明白如何处理我的项目。我是安卓工作室的新手
      • @SUJOYROY 实际上,您必须了解自定义 ArrayAdapters。需要更好的教程。看这个 - youtube.com/watch?v=qK4gQ2aOpw0
      • 这是用于图像+文本的。在您的情况下,它将是模型类中的标题+日期。
      猜你喜欢
      • 1970-01-01
      • 2020-07-30
      • 2018-10-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 2023-04-10
      相关资源
      最近更新 更多