Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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 一个适配器中有多个视图(充气机)_Java_Android_Adapter - Fatal编程技术网

Java 一个适配器中有多个视图(充气机)

Java 一个适配器中有多个视图(充气机),java,android,adapter,Java,Android,Adapter,如何在一个适配器中使用两个不同的视图?我需要一个布局的3个字段和另一个布局的第4个字段 如果屏幕上只有1-5个项目(扩展基本适配器),则此代码可以正常工作: 但当有超过5个项目时,我向下滚动,我有一个错误: ((TextView)view.findViewById(R.id.addProductTextView)).setText(p.name)中的空指针异常 您需要实现getViewTypeCount()和getItemViewType()来教导AdapterView关于您的多种行类型 例如,

如何在一个适配器中使用两个不同的视图?我需要一个布局的3个字段和另一个布局的第4个字段

如果屏幕上只有1-5个项目(扩展基本适配器),则此代码可以正常工作:

但当有超过5个项目时,我向下滚动,我有一个错误:

((TextView)view.findViewById(R.id.addProductTextView)).setText(p.name)中的空指针异常


您需要实现
getViewTypeCount()
getItemViewType()
来教导
AdapterView
关于您的多种行类型

例如,下面是一个带有
ListAdapter
的活动,它的标题和详细信息有单独的布局:

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.headerdetail;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class HeaderDetailListDemo extends ListActivity {
  private static final String[][] items= {
      { "lorem", "ipsum", "dolor", "sit", "amet" },
      { "consectetuer", "adipiscing", "elit", "morbi", "vel" },
      { "ligula", "vitae", "arcu", "aliquet", "mollis" },
      { "etiam", "vel", "erat", "placerat", "ante" },
      { "porttitor", "sodales", "pellentesque", "augue", "purus" } };

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setListAdapter(new IconicAdapter());
  }

  class IconicAdapter extends BaseAdapter {
    @Override
    public int getCount() {
      int count=0;

      for (String[] batch : items) {
        count+=1 + batch.length;
      }

      return(count);
    }

    @Override
    public Object getItem(int position) {
      int offset=position;
      int batchIndex=0;

      for (String[] batch : items) {
        if (offset == 0) {
          return(Integer.valueOf(batchIndex));
        }

        offset--;

        if (offset < batch.length) {
          return(batch[offset]);
        }

        offset-=batch.length;
        batchIndex++;
      }

      throw new IllegalArgumentException("Invalid position: "
          + String.valueOf(position));
    }

    @Override
    public long getItemId(int position) {
      return(position);
    }

    @Override
    public int getViewTypeCount() {
      return(2);
    }

    @Override
    public int getItemViewType(int position) {
      if (getItem(position) instanceof Integer) {
        return(0);
      }

      return(1);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (getItemViewType(position) == 0) {
        return(getHeaderView(position, convertView, parent));
      }

      View row=convertView;

      if (row == null) {
        row=getLayoutInflater().inflate(R.layout.row, parent, false);
      }

      ViewHolder holder=(ViewHolder)row.getTag();

      if (holder == null) {
        holder=new ViewHolder(row);
        row.setTag(holder);
      }

      String word=(String)getItem(position);

      if (word.length() > 4) {
        holder.icon.setImageResource(R.drawable.delete);
      }
      else {
        holder.icon.setImageResource(R.drawable.ok);
      }

      holder.label.setText(word);
      holder.size.setText(String.format(getString(R.string.size_template),
                                        word.length()));

      return(row);
    }

    private View getHeaderView(int position, View convertView,
                               ViewGroup parent) {
      View row=convertView;

      if (row == null) {
        row=getLayoutInflater().inflate(R.layout.header, parent, false);
      }

      Integer batchIndex=(Integer)getItem(position);
      TextView label=(TextView)row.findViewById(R.id.label);

      label.setText(String.format(getString(R.string.batch),
                                  1 + batchIndex.intValue()));

      return(row);
    }
  }
}
/***
版权所有(c)2008-2012 Commonware,LLC
根据Apache许可证2.0版(以下简称“许可证”)获得许可;你不可以
除非符合许可证,否则请使用此文件。你可以得到一份
许可证的有效期在http://www.apache.org/licenses/LICENSE-2.0. 除非需要
根据适用法律或书面同意,根据
许可证按“原样”分发,无任何保证或条件
任何形式的,无论是明示的还是暗示的。请参阅许可证以了解具体的信息
管理许可证下的权限和限制的语言。
从《忙碌的程序员指南》到Android开发_
http://commonsware.com/Android
*/
包com.commonware.android.headerdetail;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
公共类HeaderDetailistDemo扩展ListActivity{
私有静态最终字符串[][]项={
{“lorem”、“ipsum”、“dolor”、“sit”、“amet”},
{“concertetuer”、“adipising”、“elit”、“morbi”、“vel”},
{“ligula”,“vitae”,“arcu”,“aliquet”,“mollis”},
{“etiam”、“vel”、“erat”、“Placelat”、“ante”},
{“波尔蒂托”、“苏打水”、“佩伦茨克”、“奥古斯”、“普卢斯”};
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setListAdapter(新图标适配器());
}
类IconicAdapter扩展了BaseAdapter{
@凌驾
public int getCount(){
整数计数=0;
对于(字符串[]批处理:项){
计数+=1+批次长度;
}
返回(计数);
}
@凌驾
公共对象getItem(int位置){
int偏移=位置;
int batchIndex=0;
对于(字符串[]批处理:项){
如果(偏移量==0){
返回值(整数.valueOf(batchIndex));
}
偏移量--;
if(偏移量<批次长度){
退货(批量[抵销]);
}
偏移量-=批次长度;
batchIndex++;
}
抛出新的IllegalArgumentException(“无效位置:”
+字符串.valueOf(position));
}
@凌驾
公共长getItemId(int位置){
返回(位置);
}
@凌驾
public int getViewTypeCount(){
返回(2);
}
@凌驾
public int getItemViewType(int位置){
if(getItem(position)instanceof Integer){
返回(0);
}
申报表(1);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
如果(getItemViewType(位置)==0){
返回(getHeaderView(位置、转换视图、父级));
}
视图行=转换视图;
if(行==null){
行=GetLayoutFlater()。充气(R.layout.row,父级,false);
}
ViewHolder=(ViewHolder)row.getTag();
if(holder==null){
支架=新的视图支架(行);
row.setTag(支架);
}
字符串字=(字符串)getItem(位置);
if(word.length()>4){
holder.icon.setImageResource(R.drawable.delete);
}
否则{
holder.icon.setImageResource(R.drawable.ok);
}
holder.label.setText(word);
holder.size.setText(String.format(getString)(R.String.size_模板),
单词长度();
返回(行);
}
私有视图getHeaderView(内部位置,视图转换视图,
视图组(父级){
视图行=转换视图;
if(行==null){
行=GetLayoutFlater()。充气(R.layout.header,父项,false);
}
整数batchIndex=(整数)getItem(位置);
TextView标签=(TextView)row.findViewById(R.id.label);
label.setText(String.format(getString(R.String.batch)),
1+batchIndex.intValue());
返回(行);
}
}
}
getViewTypeCount()
返回
2
,因为我有两种类型的行
getItemViewType()
基于
getItem()
方法的结果,返回头行的
0
和细节行的
1
,该方法将
Integer
作为头的模型数据,并从我的数组中选择
字符串作为细节行的模型数据。

它将帮助您
/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.headerdetail;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class HeaderDetailListDemo extends ListActivity {
  private static final String[][] items= {
      { "lorem", "ipsum", "dolor", "sit", "amet" },
      { "consectetuer", "adipiscing", "elit", "morbi", "vel" },
      { "ligula", "vitae", "arcu", "aliquet", "mollis" },
      { "etiam", "vel", "erat", "placerat", "ante" },
      { "porttitor", "sodales", "pellentesque", "augue", "purus" } };

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setListAdapter(new IconicAdapter());
  }

  class IconicAdapter extends BaseAdapter {
    @Override
    public int getCount() {
      int count=0;

      for (String[] batch : items) {
        count+=1 + batch.length;
      }

      return(count);
    }

    @Override
    public Object getItem(int position) {
      int offset=position;
      int batchIndex=0;

      for (String[] batch : items) {
        if (offset == 0) {
          return(Integer.valueOf(batchIndex));
        }

        offset--;

        if (offset < batch.length) {
          return(batch[offset]);
        }

        offset-=batch.length;
        batchIndex++;
      }

      throw new IllegalArgumentException("Invalid position: "
          + String.valueOf(position));
    }

    @Override
    public long getItemId(int position) {
      return(position);
    }

    @Override
    public int getViewTypeCount() {
      return(2);
    }

    @Override
    public int getItemViewType(int position) {
      if (getItem(position) instanceof Integer) {
        return(0);
      }

      return(1);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (getItemViewType(position) == 0) {
        return(getHeaderView(position, convertView, parent));
      }

      View row=convertView;

      if (row == null) {
        row=getLayoutInflater().inflate(R.layout.row, parent, false);
      }

      ViewHolder holder=(ViewHolder)row.getTag();

      if (holder == null) {
        holder=new ViewHolder(row);
        row.setTag(holder);
      }

      String word=(String)getItem(position);

      if (word.length() > 4) {
        holder.icon.setImageResource(R.drawable.delete);
      }
      else {
        holder.icon.setImageResource(R.drawable.ok);
      }

      holder.label.setText(word);
      holder.size.setText(String.format(getString(R.string.size_template),
                                        word.length()));

      return(row);
    }

    private View getHeaderView(int position, View convertView,
                               ViewGroup parent) {
      View row=convertView;

      if (row == null) {
        row=getLayoutInflater().inflate(R.layout.header, parent, false);
      }

      Integer batchIndex=(Integer)getItem(position);
      TextView label=(TextView)row.findViewById(R.id.label);

      label.setText(String.format(getString(R.string.batch),
                                  1 + batchIndex.intValue()));

      return(row);
    }
  }
}