Question

I use such a mechanism in my application: get random string from string array and put it in the SQLite database. Then I send this new string to the widget and to ArrayList for ListActivity representation. Before the putting new object to the database I need to check if it is no object with the same text field value. My Quote object:

    public class Quote {
private String text;
private String date;
        public Quote(String text, String date)
        {
            this.text = text;
            this.date = date;
        }
        public Quote(){}
        public String getText()
        {return this.text;}
        public String getDate()
        {return this.date;}
        public void setText(String text)
        {
            this.text = text;
        }
        public void setDate(String date)
        {
            this.date = date;
        }
          @Override
            public String toString() {
                return text + "" + date;

            }

          @Override
          public boolean equals(Object obj) {
                 if (this == obj)
                     return true;
                 if (obj == null)
                     return false;
                 if (getClass() != obj.getClass())
                     return false;
                 final Quote other = (Quote) obj;
                 if (text.equalsIgnoreCase(other.text))
                     return false;
                 return true;
             }
          @Override
          public int hashCode() {
              int hash = 7;
                hash = 89 * hash + (this.text != null ? this.text.hashCode() : 0);
                hash = 89 * hash + (this.date != null ? this.date.hashCode() : 0);
                return hash;
            }

}

and this is how I get my random string with checking mechanism:

private String getRandomString(Context context)
  {
      String l  = "";
      String[] a = context.getResources().getStringArray(R.array.quotes); 
      l  = a[rgenerator.nextInt(a.length)];
      Quote quote = new Quote(l,mydate);
   ArrayList<Quote> intermediate =  getQuotes(context);
   if (intermediate.contains(quote))
    l=getRandomString(context);

     return l;
  }

but when I do it like this, I've got StackOverflowError. How can I solve it?

Was it helpful?

Solution 2

I solved this by Singletone pattern using.

public class StringSingletone {
static int i;
String []a;
private static StringSingletone instance;
LinkedList<String> quotes ;
// Private constructor prevents instantiation from other classes
private StringSingletone(Context context) {

    a = context.getResources().getStringArray(R.array.quotes);
    quotes = new LinkedList<String>(Arrays.asList(a));
    Collections.shuffle(quotes);
}

/**
 * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
 * or the first access to SingletonHolder.INSTANCE, not before.
 */

public static StringSingletone getInstance() {
    return instance;
}
public static void initInstance(Context context)
  {
    if (instance == null)
    {
      // Create the instance
      instance = new StringSingletone(context);
    }
  }
private void remakeObj(Context context)
{
    a = context.getResources().getStringArray(R.array.quotes);
    quotes = new LinkedList<String>(Arrays.asList(a));
    Collections.shuffle(quotes);

}
public String getRandomString(Context context)
{
              if (quotes.size()==0)
              {
                  remakeObj( context);
              }
              String l  = quotes.pop();
             return l;

}
}

OTHER TIPS

For this, I would recommend using Collections.shuffle().

(http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List))

So, something like:

String[] a = context.getResources().getStringArray(R.array.quotes); 
LinkedList<String> quotes = new LinkedList<String>(Arrays.asList(a))
Collections.shuffle(quotes);

Then you can just pull quotes out like this:

String aRandomQuote = quotes.pop();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top