如何将对象添加到ArrayList?

时间:2013-03-11 17:36:00

标签: android object arraylist containers

我试图从SQLite-db中读取,将结果放入自定义类型Container的Object中,将Container的内容放入ArrayList中,我可以构建由arraylist填充的listview。 我被困在我尝试将填充的Container-object传递给ArrayList的位置:

ArrayList<Container> myListContent = new ArrayList<Container>();
{ 
Container conSelected = db.readContainer(i);

int s1 = conSelected.getId();
Log.i("LISTE", "id = " + s1);

String s2 = conSelected.getVorname();
Log.i("LISTE", "vorname = " + s2);

String s3 = conSelected.getName();
Log.i("LISTE", "name = " + s3);

// Container to the ArrayList  
//THIS NEXT COMMAND IS TROUBLING ME. ALL THREE GIVE THE SAME LOG
//myListContent.add(conSelected);
//myListContent.add(new Container(conSelected.getId(), conSelected.getVorname(), conSelected.getName()));
//myListContent.add(new Container(conSelected));

Log.i("LISTE", "line = " + myListContent); 
}

//this.myAdapter = new ListActivity(this, R.layout.row, myListContent); 

我的Logcat说:

03-11 18:17:43.336: I/LISTE(7477): id = 1
03-11 18:17:43.336: I/LISTE(7477): vorname = Fred
03-11 18:17:43.336: I/LISTE(7477): name = Einsner
03-11 18:17:43.336: I/LISTE(7477): line = [null]
...
03-11 18:17:43.375: I/LISTE(7477): id = 6
03-11 18:17:43.375: I/LISTE(7477): vorname = Wolf
03-11 18:17:43.375: I/LISTE(7477): name = Sechsland
03-11 18:17:43.375: I/LISTE(7477): line = [null, null, null, null, null, null]

任何人都可以帮我这个吗?我还是个菜鸟:(

2 个答案:

答案 0 :(得分:1)

    ArrayList<Container> myListContent = new ArrayList<Container>();

   { 
    Container conSelected = db.readContainer(i);
    // Container to the ArrayList  
    myListContent.add(conSelected);

    Log.i("LISTE", "line = " + myListContent); 
    }

答案 1 :(得分:1)

即使已经选择了答案,我也想出了什么是错误,并希望将其放在这里。

如果查看Log.i,该方法将作为参数(String,String),因为他传递了这一行Log.i("LISTE", "line = " + myListContent);

myListContent遭受String强制转换,导致null值。与传递System.out.println(myListContent)"

值的Content.ToString()不同