Android 在listview中使用复杂的标题布局
我有一个android应用程序,它用自定义适配器填充listview。此适配器将节添加到具有标题的listview。此适配器如下所示:Android 在listview中使用复杂的标题布局,android,listview,Android,Listview,我有一个android应用程序,它用自定义适配器填充listview。此适配器将节添加到具有标题的listview。此适配器如下所示: public class SeparatedListAdapter extends BaseAdapter { public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>(); public final ArrayAd
public class SeparatedListAdapter extends BaseAdapter {
public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;
public SeparatedListAdapter(Context context) {
headers = new ArrayAdapter<String>(context, R.layout.list_header);
}
public void addSection(String section, Adapter adapter) {
this.headers.add(section);
this.sections.put(section, adapter);
}
public Object getItem(int position) {
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return section;
if(position < size) return adapter.getItem(position - 1);
// otherwise jump into next section
position -= size;
}
return null;
}
public int getCount() {
// total together all sections, plus one for each section header
int total = 0;
for(Adapter adapter : this.sections.values())
total += adapter.getCount() + 1;
return total;
}
public int getViewTypeCount() {
// assume that headers count as one, then total all sections
int total = 1;
for(Adapter adapter : this.sections.values())
total += adapter.getViewTypeCount();
return total;
}
public int getItemViewType(int position) {
int type = 1;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return TYPE_SECTION_HEADER;
if(position < size) return type + adapter.getItemViewType(position - 1);
// otherwise jump into next section
position -= size;
type += adapter.getViewTypeCount();
}
return -1;
}
public boolean areAllItemsSelectable() {
return false;
}
public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionnum = 0;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return headers.getView(sectionnum, convertView, parent);
if(position < size) return adapter.getView(position - 1, convertView, parent);
// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
}
public类SeparatedListAdapter扩展了BaseAdapter{
公共最终映射节=新建LinkedHashMap();
公共最终阵列适配器标头;
公共最终静态整型\节\头=0;
公共分隔的ListAdapter(上下文){
headers=newarrayadapter(上下文,R.layout.list_头);
}
public void addSection(字符串部分,适配器){
此.headers.add(节);
此.sections.put(节,适配器);
}
公共对象getItem(int位置){
对于(对象节:this.sections.keySet()){
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0)返回段;
if(位置<大小)返回适配器.getItem(位置-1);
//否则跳到下一节
位置-=大小;
}
返回null;
}
public int getCount(){
//所有章节合计,每个章节标题加上一个章节
int-total=0;
对于(适配器:this.sections.values())
总计+=适配器.getCount()+1;
返回总数;
}
public int getViewTypeCount(){
//假设头数为一,然后合计所有部分
整数合计=1;
对于(适配器:this.sections.values())
总计+=适配器。getViewTypeCount();
返回总数;
}
public int getItemViewType(int位置){
int型=1;
对于(对象节:this.sections.keySet()){
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
if(position==0)返回类型\u节\u头;
if(位置<大小)返回类型+适配器.getItemViewType(位置-1);
//否则跳到下一节
位置-=大小;
类型+=适配器。getViewTypeCount();
}
返回-1;
}
公共布尔值areAllItemsSelectable(){
返回false;
}
公共布尔值isEnabled(int位置){
返回(getItemViewType(位置)!=类型\节\标题);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父级){
int sectionnum=0;
对于(对象节:this.sections.keySet()){
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
if(position==0)返回headers.getView(sectionnum,convertView,parent);
if(position
然后由以下代码调用:
SeparatedListAdapter adapter;
public Map<String,?> createItem(String name) {
Map<String,String> item = new HashMap<String,String>();
item.put(ITEM_VALUE, name);
return item;
}
List<Map<String,?>> item = new LinkedList<Map<String,?>>();
item.add(createItem("value"));
adapter.addSection("Header", new SimpleAdapter(this, item, R.layout.list_item, new String[] { "value" }, new int[] { R.id.lblValue }));
listview.setAdapter(adapter);
分离列表适配器;
公共映射createItem(字符串名称){
Map item=newhashmap();
item.put(item_值、名称);
退货项目;
}
列表项=新建LinkedList();
添加(createItem(“值”));
addSection(“Header”,新的SimpleAdapter(this,item,R.layout.list_item,新字符串[]{“value”},新int[]{R.id.lblValue}));
setAdapter(适配器);
现在,直到最近,我的list\u头
xml文件还有一个TextView
根元素。在这一点上,它是完美的工作。但后来我想在标题上展开,以包含多个视图。list\u头的新根元素现在是一个RelativeLayout
现在,当我尝试启动该活动时,出现以下异常:IllegalStateException:ArrayAdapter要求资源ID为文本视图
。我理解这个错误,但由于我对安卓系统比较陌生,我不确定如何解决这个问题。如何修改适配器以处理包含RelativeLayout
作为根元素的标题,而不是TextView
编辑:
我相信答案在于适配器的构造函数方法
编辑:我相信答案在于
适配器
准确地说,您应该使用:
headers=newarrayadapter(上下文,R.layout.list_头,R.id.u文本视图的_id_);
还有,看一看
headers = new ArrayAdapter<String>(context, R.layout.list_header, R.id.the_id_of_textview);