Java 跨类检索ListView到适配器
我正在尝试从MainActivity向ListView/适配器添加项目,并在ListHelper中使用它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
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。谢谢你的帮助。