0

I'm trying to create list of arrays, dynamic in size, add elements and iterate through them when I finish. Currently my code is:

    String activities = "SELECT DISTINCT phoneactivity from ContextTable030313";
    ArrayList<String> myActivities = new ArrayList();

    Cursor cursor2 = db.rawQuery(activities, null);
    // looping through all rows and adding to list
    if (cursor2.moveToFirst()) {
        do {
            myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));

        } while (cursor2.moveToNext());
    }

Yet it fails to run in the do loop. I believe I am declaring something incorrectly and I get the following warnings:

- ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
    - Avoid object allocations during draw/layout operations (preallocate and reuse instead)
    - Type safety: The expression of type ArrayList needs unchecked conversion to conform to 
     ArrayList<String>

yet I do not understand why this does not work.

5
  • 1
    pre Java 7, <String> should come in both sides. ie new ArrayList<String>() as well Commented Mar 5, 2013 at 6:38
  • I did that and I still get the following warning: "Avoid object allocations during draw/layout operations (preallocate and reuse instead)" AND my code still fails in the do loop.... Commented Mar 5, 2013 at 6:41
  • check this stackoverflow.com/questions/11490611/… Commented Mar 5, 2013 at 6:42
  • It sounds like that warning is telling you that you're accessing the database from the wrong place/thread. Maybe if you hit the database and initialize myActivities in the correct place, your loop will work better. That being said, I have no idea what framework/platform you're using... Swing? Android? Commented Mar 5, 2013 at 6:43
  • Try to print cursor.getString(cursor2.getColumnIndex("ts")) in your console what exactly printing this line. Commented Mar 5, 2013 at 6:43

2 Answers 2

1

The problem seems to be this:

Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayList

Which points out to this line of code:

ArrayList<String> myActivities = new ArrayList();

You should change that line to this:

ArrayList<String> myActivities = new ArrayList<String>();

On a different note, you could replace this code segment:

  if (cursor2.moveToFirst()) {
        do {
            myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));

        } while (cursor2.moveToNext());
    }

With this:

  while (cursor2.moveToFirst()) {       
        myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));       
    }
Sign up to request clarification or add additional context in comments.

Comments

0

change your arrayList declaration such that it accepts parameterized type during constructor invokation.

ArrayList<String> myActivities = new ArrayList<String>();

If you use java 7 or above:

ArrayList<String> myActivities = new ArrayList<>();

You don't need to declare the parametreized type during constructor invokation due to *Type inference*but you still need to declare empty <>.

1 Comment

I did that and I still get the following warning: "Avoid object allocations during draw/layout operations (preallocate and reuse instead)" AND my code still fails in the do loop....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.