Mani kandan Mani kandan - 5 months ago 28
Android Question

Using loop or else to set Imageview using array

I have two arrays and two

ImageView
s. I would like to set the arrays as images in the respective
ImageView
s. Condition, using loop or else to set images into imageview. both arrays having 2 images.

Answer

//1st Array list

    ArrayList<Integer> f1Child1=new ArrayList<Integer>();
    f1Child1.add(R.drawable.animals);
    f1Child1.add(0);
    flags1.add(f1Child1);

    ArrayList<Integer> f1Child2=new ArrayList<Integer>();
    f1Child2.add(R.drawable.birds);
    f1Child2.add(1);
    flags1.add(f1Child2);

//2nd Array list

    ArrayList<Integer> f2Child1=new ArrayList<Integer>();
    f2Child1.add(R.drawable.birds);
    f2Child1.add(1);
    flags2.add(f2Child1);

    ArrayList<Integer> f2Child2=new ArrayList<Integer>();
    f2Child2.add(R.drawable.aquatic);
    f2Child2.add(0);
    flags2.add(f2Child2);

    listOfFlags.add(flags1);
    listOfFlags.add(flags2);

    final List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
    final  String[] from = { "flag"};
    final  int[] to = { R.id.flag};

//Load 1st Grid view

    final GridView gridview = (GridView) findViewById(R.id.gridview1);

    for(int i=0;i<listOfFlags.get(0).size();i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("flag", Integer.toString(listOfFlags.get(0).get(i).get(0)));
        flags1.get(i);
        aList.add(hm);
    }

//using simple adapter to set gridview

    SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridviewdata, from, to);
    gridview.setAdapter(simpleAdapter);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {

            aList.clear();

//use popup or dialog

            if (listOfFlags.get(increament).get(position).get(1) == 1) {
                ViewDialog alert = new ViewDialog();
                alert.showDialog(BirdQuiz.this, "");
                correct += 1;
                result += 100;
            }
            else {
                ViewDialog1 alert1 = new ViewDialog1();
                alert1.showDialog(BirdQuiz.this, "");
                wrong +=1;
            }

//if needed pass intent value

            if (increament < listOfFlags.size()-1) {
                increament++;
            }
            else{
                Intent resultAl = new Intent(BirdQuiz.this, Result.class);
                resultAl.putExtra("Correct", correct);
                resultAl.putExtra("Result", result);
                resultAl.putExtra("Wrong", wrong);
                startActivity(resultAl);
                finish();
            }

//After a click load next gridview

            for (int i = 0; i < listOfFlags.get(increament).size(); i++) {
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("flag", Integer.toString(listOfFlags.get(increament).get(i).get(0)));
                aList.add(hm);
            }

            SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridviewdata, from, to);
            gridview.setAdapter(simpleAdapter);
        }
    });