开发者

Android ListView filled with String[]

开发者 https://www.devze.com 2023-04-12 19:14 出处:网络
I\'m trying to fill a ListView with a String[]. The problem is that i want to make this filling when i find occurences with another array.

I'm trying to fill a ListView with a String[]. The problem is that i want to make this filling when i find occurences with another array. I try to explain. I have an ArrayList [0,1,0,0,1,...] This changes everytime an user give some answers to a quiz session. Example: 10 questions, 10 answers and so ArrayList's size = 10. As you can see 0 is for "wrong answer" and 1 is for "correct answer". So i create an ArrayList with the maximum size depending on database's quiz table and this is called "domandesatte".

So i made this 2 String[]: one for correct answer ("Domanda #: Esatta means Question # : CORRECT) and one for wrong answer ("Domanda #: ERRATA!" means "Question #: Wrong")

Finally "vettoreint" is the ArrayList which i get from Intent.getIntegerArrayListExtra();

listView = (ListView)findViewById(R.id.list2);
      listView.setTextFilterEnabled(true);
        listView2 = (ListView)findViewById(R.id.listSbagliato);
          list开发者_开发问答View2.setTextFilterEnabled(true);
final String[] domandesatte = {"Domanda n°1: ESATTA!","Domanda n°2: ESATTA!","Domanda n°3: ESATTA!",
            "Domanda n°4: ESATTA!","Domanda n°5: ESATTA!","Domanda n°6: ESATTA!",
            "Domanda n°7: ESATTA!","Domanda n°8: ESATTA!","Domanda n°9: ESATTA!",
            "Domanda n°10: ESATTA!","Domanda n°11: ESATTA!","Domanda n°12: ESATTA!",
            "Domanda n°13: ESATTA!","Domanda n°14: ESATTA!","Domanda n°15: ESATTA!",
            "Domanda n°16: ESATTA!","Domanda n°17: ESATTA!","Domanda n°18: ESATTA!",
            "Domanda n°19: ESATTA!","Domanda n°20: ESATTA!","Domanda n°21: ESATTA!",
            "Domanda n°22: ESATTA!","Domanda n°23: ESATTA!"};

final String[] domanderrate = {"Domanda n°1: ERRATA!","Domanda n°2: ERRATA!","Domanda n°3: ERRATA!",
            "Domanda n°4: ERRATA!","Domanda n°5: ERRATA!","Domanda n°6: ERRATA!",
            "Domanda n°7: ERRATA!","Domanda n°8: ERRATA!","Domanda n°9: ERRATA!",
            "Domanda n°10: ERRATA!","Domanda n°11: ERRATA!","Domanda n°12: ERRATA!",
            "Domanda n°13: ERRATA!","Domanda n°14: ERRATA!","Domanda n°15: ERRATA!",
            "Domanda n°16: ERRATA!","Domanda n°17: ERRATA!","Domanda n°18: ERRATA!",
            "Domanda n°19: ERRATA!","Domanda n°20: ERRATA!","Domanda n°21: ERRATA!",
            "Domanda n°22: ERRATA!","Domanda n°23: ERRATA!"};

for(int i=0;i<vettoreint.size();i++)
        {
     if (vettoreint.get(i) == 1) {

         listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, domandesatte));
        listView.setTextFilterEnabled(true);    
     }
     else {
         if(vettoreint.get(i) == 0)
         {

         listView2.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, domandesatte));
        listView2.setTextFilterEnabled(true);   

         }
        }
        }

All i want to do is to see 2 ListViews (1 for correct answer and 1 for wrong answer) depending on the ArrayList Example: 10 Answers, 5 "Question # Correct", 5 "Question # Wrong".

I try to put domandesatte[i] but it is not correct according to Adapter. So i tried to put subList(i,i+1) but it returns only the last two occurrences: one for correct and one for wrong.

Do you have any suggests? sorry for my english and thank you for all your great support!


I don't know if i'm understanding your problem but couldn't you do it like this:

private ArrayList<String> domandesatte = new ArrayList<String>();
private ArrayList<String> domanderrate = new ArrayList<String>();

....

for(int i=0;i < vettoreint.size();i++) {
     if (vettoreint.get(i) == 1) {
         domandesatte.add("Domanda n°" + i + ": ESATTA!");
     } else {
         domanderrate.add("Domanda n°" + i + ": ERRATA!");
     }
}

listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, domandesatte));

listView2.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, domanderatte));
0

精彩评论

暂无评论...
验证码 换一张
取 消