dimanche 16 août 2015

Converting simple variable function to an arraylist function in android

I am developing an app where I have to generate random values from an string arraylist “obj” and saved the generated values to another arraylist “rndmObj”. I need to then display all the values at once to the screen(I don’t know here the textview will be used or what).

The below code is not what I want. The “randObjFunc()” function generated a value from arraylist “obj” and save one value to a variable called “item”. And then display the value of “item” to a textview named “tvObjectList1”. Actually the function I wrote ran whenever the onclicklistener of each object is called whenever the user touches that object. Long story short is, I just want to save random values to another arraylist "rndmObj" at once and then display all the values to a textview at once(again: i don't know whatever the structure is used to display multiple strings on the screen). Hope the description is clear. Please do comment the modified code snippet of “randObjFunc()” and whatever else needed!

   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.level);

   //calling basic functions to run the class properly from within the        onCreate function
    prepareObjList();
    randObjFunc();
    //dispRndmObj();


   //creating a timer here
    tvTimer = (TextView) findViewById(R.id.tvTimer);



     // Pinning textViews and imageViews with their appropriate resources in the .xml file
    tvResult = (TextView)findViewById(R.id.tvResult);
    tvObjectList1 = (TextView) findViewById (R.id.tvObjectList1);
    tvObjectList2 = (TextView) findViewById (R.id.tvObjectList2);



    bowTie = (ImageView) findViewById(R.id.bowtie);
    bowTie.setVisibility(View.VISIBLE);
    key = (ImageView) findViewById(R.id.key);
    key.setVisibility(View.VISIBLE);
    grosshoper = (ImageView) findViewById(R.id.grasshoper);
    grosshoper.setVisibility(View.VISIBLE);


   // OnClickListeners for each imageView
    bowTie.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            v.setVisibility(View.INVISIBLE);
            objectCount--;
            checkObjectCount();

            int position = obj.indexOf("bowTie");
            obj.remove(position);

            randObjFunc();

        }
    });
    //---------------------------------------------------------------------------   
    key.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            v.setVisibility(View.INVISIBLE);
            objectCount--;
            checkObjectCount();

            int position = obj.indexOf("key");
            obj.remove(position);

            randObjFunc();

        }
    });
    //---------------------------------------------------------------------------       
    grosshoper.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            v.setVisibility(View.INVISIBLE);
            objectCount--;
            checkObjectCount();

            int position = obj.indexOf("grosshoper");
            obj.remove(position);

            randObjFunc();

        }
    });
     //---------------------------------------------------------------------------  
     } //onCreateBoundary Ended Here!   


   // defining prepareObjList() method and papulating obj arrayListt within the method
    private void prepareObjList() {
    // TODO Auto-generated method stub
    //adding string values to arrayList for tvObjectList1
    obj = new ArrayList<String>();
    obj.add("bowTie");
    obj.add("key");
    obj.add("grosshoper");


    //adding imageViews to objImg arrayList
    objImg = new ArrayList<Integer>();
    objImg.add(R.drawable.level1_bowtie);
    objImg.add(R.drawable.level1_key);
    objImg.add(R.drawable.level1_grasshopper);
   }

     // defining randObjFunc() for randomly displaying objectNames on the TvObjList1 textview
   private void randObjFunc() {
    // TODO Auto-generated method stub
    //set the size of list as the max number for the generator
    //get a random number and retrieving element
    //using public variable "item" to store random value getting from obj
      item = obj.get(randomGenerator.nextInt(obj.size()));
    //set random element to a textview
        ((TextView) findViewById(R.id.tvObjectList1)).setText(item);
        findingObjects++;
      //appending item name with level initial for finding the object from the objImg arraylist
      item = ( "level1_" + item );  
    }




Aucun commentaire:

Enregistrer un commentaire