【发布时间】:2015-01-23 18:11:18
【问题描述】:
我有一个包含各种元素的列表视图,并且更新了很多次。 我想在一些随机位置之后添加一条分隔线,例如在第 3 个元素、第 7 个元素、第 17 个元素、第 19 个元素等之后。
所以我设计了一个包含分隔符的 xml 文件,并使用自定义 SimpleAdapter 在这些位置上填充它。但是,当我运行我的代码时,分隔符被添加到奇怪的位置,使我的列表看起来太难看,有时会给出Unfortunately, yourApp has stopped 错误
我以前从未使用过自定义适配器,也不知道我做错了什么。
这是我的简单代码,我已经添加了 cmets 进行解释,
public class SpecialAdapter extends SimpleAdapter {
Context context;
private LayoutInflater mInflater;
public SpecialAdapter(Context context, ArrayList<HashMap<String, Object>> newsList, int resource,String[] from, int[] to) {
super(context, newsList, resource, from, to);
this.context = context;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// TestApp.endPos holds the positions (it keep changing) after which i want to add
// separator
if (position == TestApp.endPos){
/* my Arraylist is of type HashMap<String, Object>
* I have added an empty element and add the seperator view in
* it.
*/
TestApp.arrayList.add(new HashMap<String, Object>());
// heads is an xml file of my separator
view = mInflater.inflate(R.layout.heads, null);
}
return view;
}
}
谁能帮助我如何简化我的问题以及我做错了什么?
【问题讨论】:
标签: android listview android-listview simpleadapter