Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列表视图不显示_Java_Android_Listview_Display - Fatal编程技术网

Java 列表视图不显示

Java 列表视图不显示,java,android,listview,display,Java,Android,Listview,Display,我有一个包含唯一id的整数数组列表 我有这个方法来填充listview public void getDataDetails(int id) { Cursor c = db.getData(id); String[] from = new String[]{ID, name}; int[] to = new int[]{R.id.layout_id, R.id.layout_name}; S

我有一个包含唯一id的整数数组列表

我有这个方法来填充listview

public void getDataDetails(int id)
    {
            Cursor c = db.getData(id);
            String[] from = new String[]{ID, name};
            int[] to = new int[]{R.id.layout_id, R.id.layout_name};

            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.layout, c, from, to, 0);
            listview.setAdapter(adapter);
            adapter.notifyDataSetChanged();
    }
在我的onCreate方法中,我有以下代码。变量数据是我的ArrayList,它的值为1和2

listview= (ListView) findViewById(R.id.listView);
for (int x=0; x < data.size(); x++)
{
      getDataDetails(data.get(x));
}
listview=(listview)findViewById(R.id.listview);
对于(int x=0;x
我的问题是,显示的唯一数据是x=2时的数据。应该有两个listview包含x=1和x=2中的数据。有什么帮助吗?

将其放入
onCreate()


如果我在活动中的函数中编写它,然后在onCreate中调用该函数,会有什么区别?在for循环的每次迭代中,您都在创建适配器并将其附加到listview,这是不可行的。获取arraylist中的所有数据,然后用它填充listview。在第一次迭代中,listview是填充的,但当第二次迭代执行时,它与第一次迭代重叠,这就是为什么在x=2I时显示数据的原因。感谢它的帮助,如果你不介意的话,你可以投票并更正我的答案:)
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.layout, c, from, to, 0);
listview.setAdapter(adapter);