Aug 1, 2011

Logic : Getting Random Numbers and Random Questions using Array List Class


getting Random Numbers :
=======================
public ArrayList getRandomList() {
Random rand = new Random();
ArrayList alist = new ArrayList();
while (alist.size() < 5) {
int v = rand.nextInt(5);
if (!alist.contains(v)) {
alist.add(v);
}
}
return alist;
}








Getting Random Questions from ArrayList :
-----------------------------------




public ArrayList getQuestions(int level, ArrayList listObj){
ArrayList listObj2=new ArrayList();
Random r=new Random();
while(listObj2.size()<=20)
{
int r1=r.nextInt(listObj.size());
ModuleQuesBean mq=listObj.get(r1);
int lid=Integer.parseInt(mq.getLid());
if(lid==level)
{
if(!listObj2.contains(mq));
{
listObj2.add(mq);
}
}
}
for(int i=0;i<10;i++)
{
Log.w(" array list values ",listObj2.get(i).getQuestionid()+"");
}
return listObj2;
}

9 comments:

  1. can u give clear explanation... i am getting confuse. please explain total code here

    ReplyDelete
  2. getting Random Numbers :
    =======================
    public ArrayList getRandomList() {
    // Random Class to provide random numbers
    Random rand = new Random();
    ArrayList alist = new ArrayList();
    while (alist.size() < 5) {
    int v = rand.nextInt(5);
    // simply i am stroring random values in Array list , before storing checking whether value are existed or not , ifr value is does not exits storing that value in array list
    if (!alist.contains(v)) {
    alist.add(v);
    }
    }
    // finally return array list which contains random values
    return alist;
    }

    ReplyDelete
  3. in android we use syntax like " ArrayList obj=new Arraylist "
    but when i am using this code i getting errors.

    and another doubt is ModuleQuestionBean is it a new class?
    and where you define getquestionid();method

    will u send me total code my mail id:shivakumar.martha@gmail.com

    ReplyDelete
    Replies
    1. ModuleQuesBean is a new Class ( it's a bean )
      which is used to store data .

      actaullay wat u want ?
      what is ur requirement ?

      Delete
  4. i want to generate random questions and ansewers...

    ReplyDelete
    Replies
    1. take one bean class store questions and answers and store that bean object in Arraylist , then try my logic

      if u want to more come to skype : vsravan1

      Delete
    2. Are there in Skype now.. i sent request u.

      Delete
  5. hi i am very new to android and java. i wanna generate non-repeated random numbers in android, which i wanna display as pyramid using number of TextViews. can anyone help me to find out the solution of it?

    ReplyDelete
    Replies
    1. for that you have to take dynamic text views ,
      may i know how many rows you wants to show in pyramid

      Delete