如何将数据从List <object>放到String数组Android?</object>

时间:2014-10-24 17:27:02

标签: java android arrays object android-arrayadapter

这是我想要插入数组的对象。 getters和setter都已定义。

public class Task {

private int id;
private String title;
private String description;

public Task(){}

public Task(String title, String description) {
    super();
    this.title = title;
    this.description = description;
}

//getters & setters
}

这是getAllTask​​s方法,它将我的数据库中的数据转换为Task对象,然后将对象转换为List。

public List<Task> getAllTasks() {
    List<Task> tasks = new LinkedList<Task>();

    // 1. build the query
    String query = "SELECT  * FROM " + TABLE_TASKS;

    // 2. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    // 3. go over each row, build task and add it to list
    Task task = null;
    if (cursor.moveToFirst()) {
        do {
            task = new Task();
            task.setId(Integer.parseInt(cursor.getString(0)));
            task.setTitle(cursor.getString(1));
            task.setDescription(cursor.getString(2));

            // Add task to tasks
            tasks.add(task);
        } while (cursor.moveToNext());
    }

    Log.d("getAllTasks()", tasks.toString());

    // return tasks
    return tasks;
}

这就是我想要表现的:

我想在String数组taskList中插入对象。

MySQLiteHelper db = new MySQLiteHelper(this);
    List<Task> tasks = db.getAllTasks();
    String [] taskList = new String[tasks.size()];
    int index = 0;
    for (Task value : tasks) {
        taskList[index] = String.valueOf( value );
        index++;
    }

1 个答案:

答案 0 :(得分:0)

您需要覆盖类toString的对象Task方法。

示例:

public String toString() {
    return "Task [id=" + id + ", title=" + title + ", description=" + description + "]";
}