如何从数组数组中填充Spinner

时间:2011-11-07 01:35:25

标签: android arrays spinner

这是我收到的错误。

空指针异常 11-06 19:03:34.050:WARN / System.err(5342):在RetrievingAmazonXMLDataActivity.onCreate(RetrievingAmazonXMLDataActivity.java:88)

    for (int i = 0; i < tokens.length; i++) {
                String a = dumpTitles("ProductName", i);
                element = a.split("!");
                allProducts.add(element);
            }

           w =  (String[][])allProducts.toArray(new String[allProducts.size()][]);


    Spinner spinnerProducts = (Spinner) findViewById(R.id.spinner2);
            spinnerProducts.setOnItemSelectedListener(this);

// **** error line below


     ArrayAdapter<String> productsArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, productArrayToShow);

           productsArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerProducts.setAdapter(productsArrayAdapter);

     public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
        {

            try {
                selected = parent.getItemAtPosition(position).toString();


                productArrayToShow = w[position];

            } catch (Exception e) {}
        }

1 个答案:

答案 0 :(得分:0)

你并没有真正说出你的代码中有什么“productArrayToShow”,但我会假设它是你想在适配器中显示的二维字符串数组。最简单的方法就是用它制作一维阵列:

int rows = productArrayToShow.length;
int cols = productArrayToShow[0].length;
String[] productArray = new String[rows*cols];
int count = 0;
for(int r=0; r<rows; ++r)
  for(int c=0; c<cols; ++c)
    productArray[count++] = productArrayToShow[r][c];