Android:通过索引获取arrayList元素

时间:2014-04-15 07:19:14

标签: java android arraylist

我有两个arraylist,我已合并他们的数据

    ArrayList prayerNames={Fajar,Zohar,Asar,Magrib,Isha};

    ArrayList prayerTime={4:04am,2:58pm,4:20pm,5:09pm,8:10pm}

    ArrayList<String> mergedList= new ArrayList<String>();
    for(int i=0;i<prayersTime.size();i++){
     mergedList.add(prayerNames.get(i)+""+prayerTimes.get(i));
     }
     fajarText.append(mergedList.get(0));
     zoharText.append(mergedList.get(2));
     asarText.append(mergedList.get(4))

但是现在当我想从具有特定索引的合并列表中获取数据时,我的活动停止了。 需要帮助

1 个答案:

答案 0 :(得分:1)

您的ArrayList初始化不正确。试试这个:

ArrayList<String> prayerNames = new ArrayList<String>();
prayerNames.add("Fajar");
prayerNames.add("Zohar");
prayerNames.add("Asar");
prayerNames.add("Magrib");
prayerNames.add("Isha");

ArrayList<String> prayerTimes = new ArrayList<String>();
prayerTimes.add("4:04am");
prayerTimes.add("2:58pm");
prayerTimes.add("4:20pm");
prayerTimes.add("5:09pm");
prayerTimes.add("8:10pm");

ArrayList<String> mergedList = new ArrayList<String>();
for (int i = 0; i < prayerTimes.size(); i++) {
    mergedList.add(prayerNames.get(i) + "" + prayerTimes.get(i));
}

fajarText.append(mergedList.get(0));
zoharText.append(mergedList.get(2));
asarText.append(mergedList.get(4));