Android 如何将对象添加到ArrayList?

Android 如何将对象添加到ArrayList?,android,object,arraylist,containers,Android,Object,Arraylist,Containers,我试图从SQLite数据库中读取,将结果放入自定义类型容器的对象中,将容器的内容放入ArrayList,我可以构建由ArrayList填充的listview。 我在尝试将已填充的容器对象传递给ArrayList时遇到了问题: ArrayList<Container> myListContent = new ArrayList<Container>(); { Container conSelected = db.readContainer(i); int s1 = co

我试图从SQLite数据库中读取,将结果放入自定义类型容器的对象中,将容器的内容放入ArrayList,我可以构建由ArrayList填充的listview。 我在尝试将已填充的容器对象传递给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); 
    ArrayList<Container> myListContent = new ArrayList<Container>();

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

    Log.i("LISTE", "line = " + myListContent); 
    }
有人能帮我吗?我还是个新手:(

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

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

    Log.i("LISTE", "line = " + myListContent); 
    }
{ Container conSelected=db.readContainer(i); //将容器添加到ArrayList myListContent.add(已选择); Log.i(“LISTE”、“line=“+myListContent”); }
即使选择了答案,我还是找出了错误所在,并希望将其放在这里。

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

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

    Log.i("LISTE", "line = " + myListContent); 
    }
如果您看一下,该方法将作为参数(String,String),因为他正在传递此行
Log.i(“LISTE”,“line=“+myListContent”);


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

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

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

myListContent遭受字符串强制转换,这会导致空值。与传递
Content.ToString()值的
System.out.println(myListContent)“
不同,myListContent会传递
Content.ToString()的值。

您能否显示myListContentPost的定义为
新容器(conSelected)提供代码
constructor.java:public Container(int-parseInt,String-vor,String-nach){this.id=parseInt;this.vorname=vor;this.nachname=nach;}这一行是什么
应该做什么?这在你试图完成的内容中毫无意义。请在你的问题中发布
readContainer()
Container#toString()
,否则可能无法帮助你。这也是我的第一个猜测,但logcat在那里得到了相同的输出:line={null,null,null}当你朝正确的方向戳我的鼻子时,我自己发现了这个问题。日志记录是个问题。你不能简单地让一个对象数组被记录下来。所以我使用StringBuilder将它转换成字符串。所以你的解决方案是正确的!:)thx是的。我想是在有人向那个方向戳我之后。问题是,堆叠“空值”的日志把我弄糊涂了!我注意到你得出了正确的结论,但正确的答案并没有指出这一点,这可能会对以后可以阅读的人造成一些误解。是的。现在切换答案:)
    ArrayList<Container> myListContent = new ArrayList<Container>();

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

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