Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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 跨类检索ListView到适配器_Java_Android_Listview_Adaptor - Fatal编程技术网

Java 跨类检索ListView到适配器

Java 跨类检索ListView到适配器,java,android,listview,adaptor,Java,Android,Listview,Adaptor,我正在尝试从MainActivity向ListView/适配器添加项目,并在ListHelper中使用它 ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(adapter); adapter = new CustomListAdapter(this, Registrant.searchList); adapter.notifyDataSetChanged(); 我已在注册人处申报 public

我正在尝试从MainActivity向ListView/适配器添加项目,并在ListHelper中使用它

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
adapter = new CustomListAdapter(this, Registrant.searchList);
adapter.notifyDataSetChanged();
我已在注册人处申报

public static List<Registrant> searchList = new ArrayList<Registrant>();
然后我尝试从ListHelper将其检索到listview中

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
adapter = new CustomListAdapter(this, Registrant.searchList);
adapter.notifyDataSetChanged();
但没有运气,这是一个空白屏幕


任何想法都会有帮助。。谢谢大家!

而不是创建
CustomListAdapter
类的对象来更新ListView数据源中的数据。在
CustomListAdapter
类中创建一个方法,用于在列表中添加适配器用作数据源的数据:

CustomListAdapter
类中添加以下方法:

public void addItems(){
   arrList.addAll(Registrant.searchList);
   this.notifyDataSetChanged();
}
现在,如果要更新ListView中的数据,请调用
addItems
方法:

listView.setAdapter(adapter); 
adapter.addItems();

而不是创建
CustomListAdapter
类的对象来更新ListView数据源中的数据。在
CustomListAdapter
类中创建一个方法,用于在列表中添加适配器用作数据源的数据:

CustomListAdapter
类中添加以下方法:

public void addItems(){
   arrList.addAll(Registrant.searchList);
   this.notifyDataSetChanged();
}
现在,如果要更新ListView中的数据,请调用
addItems
方法:

listView.setAdapter(adapter); 
adapter.addItems();

这将引导我走向正确的方向,然而,问题是我在设置适配器之前刚刚设置了ListView。感谢您的帮助。这将引导我走向正确的方向,但是,问题是我在设置适配器之前刚刚设置了ListView。谢谢你的帮助。