samedi 25 juin 2016

Recreate a random array to set into Gridview after using OnItemClickListener

I'm trying to make a simple game using a random array and put it in gridview. But after click on item of gridview, i can't generate a new array because next random array must be inside the OnItemClickListener- can't setAdapter like outside OnCreate method.I need anysolution from you guys. Here is my code:

 for (int j=1;j<25;){
        int random=((int)(Math.random()*25))+1;
        if (!list.contains(random)){
            list.add (random);
            j++;

        }
    }
     list.add(a);

    gridView = (GridView) findViewById(R.id.gridView);
    final ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
            android.R.layout.simple_list_item_1,list);

    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
           gridView=(GridView)findViewById(R.id.gridView);
            Integer values=Integer.parseInt(gridView.getItemAtPosition(position).toString());
            y.setText(String.valueOf(value));
            if (a==values){

                for (int j=1;j<25;){
                    int random=((int)(Math.random()*25))+1;
                    if (!list.contains(random)){
                        list.add (random);
                        j++;
                    }
                }
                list.add(a);



                Collections.shuffle(list);
               adapter.notifyDataSetChanged();
                a=a+r.nextInt(11);
                x.setText(String.valueOf(a));
                System.out.println(a);

                Toast.makeText(getApplicationContext(),
                        "OK", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getApplicationContext(),
                        "Failed", Toast.LENGTH_SHORT).show();
            }

        }
    });

}




Aucun commentaire:

Enregistrer un commentaire