-2

I'm new to android development in java and i don't understand one thing. I need to get data from my db (no problem for that) and stock it in a ArrayList of Object but when i put an elem, the next elememt will overwriting all the previous.

public ArrayList<Task> getTaskList(){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(DB_TABLE,new String[]{DB_TITLE, DB_DESC},null,null,null,null,null);
    ArrayList<Task> taskList = new ArrayList<>();

    if (taskList == null)
        taskList = new ArrayList<>();
    while(cursor.moveToNext()){
        int indexTitle = cursor.getColumnIndex(DB_TITLE);
        int indexDesc = cursor.getColumnIndex(DB_DESC);
        String title = cursor.getString(indexTitle);
        String desc = cursor.getString(indexDesc);

        Task task = new Task(title, desc);

        taskList.add(task);
        System.out.println("NOTRE LISTE CONTIENT");
        for (Task taask : taskList){
            System.out.println(taask.getTitle() + "-" + taask.getDescription());
        }
    }
    cursor.close();
    db.close();
    return taskList;
}

Task.Java

public class Task {
private static String title;
private static String description;

public Task(){}

public Task(String title, String description){
    this.title = title;
    this.description = description;
    System.out.println("NEW TASK: " + title + " " + description);
}

public String getTitle() { return this.title; }
public void setTitle(String title) { this.title = title; }

public String getDescription() { return this.description; }
public void setDescription(String description) { this.description = description; }

}

It's suppose to print:

test-tata potpot-eptept tata-tete test-teeest arr-arg

But print :

arr-arg arr-arg arr-arg arr-arg arr-arg

Do you have any idea? I've shearch but nothing works.. Thanks.

Sabri K
  • 11
  • 5
  • And what makes you think it's doing that? – Joe C Jan 31 '18 at 22:24
  • It's a little strange that the `System.out.println` loop is _inside_ the `cursor.moveToNext()` loop. This means that if your db contained `"A"`, `"B"`, and `"C"`, you would see output like `"A", "A", "B", "A", "B", "C"` which could be confusing you – Ben P. Jan 31 '18 at 22:26
  • @JoeC when i display the Array, it display the last elem with the array size – Sabri K Jan 31 '18 at 22:27
  • It doesn't look like it should be overwriting anything. However, you print the entire list every time you read a record. So you get Record 1, Record 1, Record 2, Record 1, Record 2, Record 3, etc. – SeverityOne Jan 31 '18 at 22:27
  • I add some info but i don't get what you say. – Sabri K Jan 31 '18 at 22:32

1 Answers1

2

The problem is in your Task class:

private static String title;
private static String description;

The static keyword essentially means "share this value between all instances". Every time you create a "new" Task instance, you update title and description, which changes the value of these variables for all instances of the Task class.

Delete the static keyword from both.

private String title;
private String description;
Ben P.
  • 52,661
  • 6
  • 95
  • 123