0

我在我的应用程序中使用了这样一种机制:从字符串数组中获取随机字符串并将其放入 SQLite 数据库中。然后我将这个新字符串发送到小部件和 ArrayList 以获取 ListActivity 表示。在将新对象放入数据库之前,我需要检查它是否不是具有相同文本字段值的对象。我的报价对象:

    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;
            }

}

这就是我通过检查机制获得随机字符串的方式:

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;
  }

但是当我这样做时,我得到了 StackOverflowError。我该如何解决?

4

2 回答 2

1

为此,我建议使用Collections.shuffle().

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

所以,像:

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

然后你可以像这样拉出引号:

String aRandomQuote = quotes.pop();
于 2013-06-17T14:49:28.800 回答
0

我通过使用 Singletone 模式解决了这个问题。

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;

}
}
于 2013-06-18T13:35:27.933 回答