所以我有一个我创建的自定义列表适配器,我试图使其动态化。它开始是空的,然后用户可以动态添加或删除项目。一切正常,除了一件事:当我更新适配器时没有调用适配器的“getView” ,因此项目的标题 - 显然 - 没有设置(而是显示它的地址)。这是更新数组的代码:
private void newTab(String name, String url){
_tabAdapter.add(new Article(name,url));
_tabAdapter.notifyDataSetChanged();
_tabCount++;
}
这是适配器本身的代码:
public class TabAdapter extends ArrayAdapter<Article>{
Context _context;
@Override
public int getCount() {
return _tabCount;
}
public TabAdapter(Context c) {
super(c, R.layout.tab_item);
_context=c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) _context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = inflater.inflate(R.layout.tab_item, parent,false);
TextView _itemText = (TextView) _view.findViewById(R.id.tab_text);
Article getArticle = getItem(position);
String newName = getArticle.getName();
_itemText.setText(newName);
return _view;
}
}
现在其他一切工作正常 - 即使 OnClick 工作得很好并且正确获取了 itme 的名称,只是列表本身没有显示项目的名称。我一定错过了一些非常基本的东西,但我找不到它是什么。任何帮助,将不胜感激。
更新:虽然这不是所提到的确切问题的解决方案,但我设法通过为“Article”类创建一个“toString()”方法来解决它 - 所以现在标题显示为它应该显示的 - 除了它不是一个非常优雅的解决方案。