Android使用2个ArrayList填充Spinner

时间:2014-06-18 18:45:36

标签: java android arraylist spinner

如何使用2个ArrayList填充微调器?例如:我有2个arrayList

<string-array name="month">
<item>Gen</item>
<item>Feb</item>
<item>Mar</item>
</string-array>

<string-array name="monthN">
<item>0</item>
<item>01</item>
<item>02</item>
</string-array>

在微调器中,我希望以这种方式看到列表:

0 Gen 01 Feb 02 Mar

现在我有一个简单的适配器

ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.month,
        android.R.layout.simple_spinner_item);
            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    monthSelector.setAdapter(arrayAdapter);

2 个答案:

答案 0 :(得分:1)

迭代String数组并将字符串连接在一起。这看起来像这样:

ArrayList<String> concatenated = new ArrayList<>();
String[] months = getResources().getStringArray(R.string.month);
String[] numbers = getResources().getStringArray(R.string.monthN);
for(int i = 0; i < months.length; i++) {
    concatenated.add(numbers[i] + " " + months[i]);
}

现在concatenated的每个元素都会显示为NUMBER NAME

答案 1 :(得分:0)

如果您希望月份出现在微调器中,并将所选值作为数字,请尝试以下操作:

 private void method name{
 ArrayList<dateStruct> datesArray = new ArrayList(dateStruct>();
 dateStruct = new dateStruct();
 String[] months = getResources().getStringArray(R.string.month);
 String[] numbers = getResources().getStringArray(R.string.monthN);
 // Build dates list and add to datesArray
 for(int i = 0; i < months.length; i++) {
    dateStruct.dateNumber = numbers[i];
    dateStruct.dateName = months[i];
    datesArray.add().dateStruct();
}

// bind to Spinner control
ArrayAdapter<dateStruct> arrayAdapter = new ArrayAdapter< (MainActivity.this, R.layout.support_simple_spinner_dropdown_item, datesArray);
taskListDropdown_control.setAdapter(arrayAdapter);
}

 public class dateStruct
   {
      public String dateNumber;
      public String dataName;
   }