ListView具有每个项目的不同布局

时间:2014-01-04 22:51:38

标签: java android xml listview

我找到了这两个答案:https://stackoverflow.com/a/11787066/1489990https://stackoverflow.com/a/18868395/1489990我一直在努力完成每个项目的不同布局的列表视图。我的数组适配器如下所示:

public class MyArrayAdapter<T> extends ArrayAdapter<T> {
    LayoutInflater mInflater;
    int[] mLayoutResourceIds;

    public MyArrayAdapter(Context context, int[] textViewResourceId, List<T> objects) {
        super(context, textViewResourceId[0], objects);
        mInflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        mLayoutResourceIds = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutInflater inflater = null;
        int type = getItemViewType(position);
        // instead of if else you can use a case
        if (row  == null) {
            if (type == TYPE_ITEM1) {
                //infalte layout of type1
                row = inflater.inflate(R.id.item1, parent, false); //*****************
            }
            if (type == TYPE_ITEM2) {
                //infalte layout of type2
            }  else {
                //infalte layout of normaltype
            }
        }
        return super.getView(position, convertView, parent);
    }

    @Override
    public int getItemViewType(int position) {
        if (position== 0){
            type = TYPE_ITEM1;
        } else if  (position == 1){
            type = TYPE_ITEM2;
        }
        else
        {
            type= TYPE_ITEM3 ;
        }
        return type;    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public int getViewTypeCount() {
        return 3;    //To change body of overridden methods use File | Settings | File Templates.
    }
}

和我的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] array = new String[] {"one", "two", "three", "four", "five", "six"};
    List<String> list = new ArrayList<String>();
    Collections.addAll(list, array);

    ListView listView = new ListView(this);
    listView.setAdapter(new MyArrayAdapter<String>(this, new int[] {android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_single_choice}, list));
    setContentView(listView);
}

这个问题是我的数组适配器中的行:

row = inflater.inflate(R.id.item1, parent, false);

给出错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference

我对xml布局的结构有点困惑。现在我有一个xml用于listView,另一个用于第一个列表布局,我尝试在1 xml布局中完成它但它仍然给出了空指针异常。我需要一些指导如何实现这一目标。感谢

2 个答案:

答案 0 :(得分:2)

您忘记正确初始化inflater。您将其明确地设置为nullLayoutInflater inflater = null;),因此在调用inflater.inflate(R.id.item1, parent, false);时,它会抛出NPE。

您是否希望使用mInflater变量?

 row = mInflater.inflate(R.id.item1, parent, false);

但是我将摆脱这个mInflater变量并直接在getView方法中执行此操作:

public class MyArrayAdapter<T> extends ArrayAdapter<T> {
    int[] mLayoutResourceIds;

    public MyArrayAdapter(Context context, int[] textViewResourceId, List<T> objects) {
        super(context, textViewResourceId[0], objects);
        mLayoutResourceIds = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        int type = getItemViewType(position);
        // instead of if else you can use a case
        if (row  == null) {
            LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (type == TYPE_ITEM1) {
                //infalte layout of type1
                row = inflater.inflate(R.id.item1, parent, false); //*****************
            }
            if (type == TYPE_ITEM2) {
                //infalte layout of type2
            }  else {
                //infalte layout of normaltype
            }
        }
        return super.getView(position, row, parent);
    }
    /**
     * Other stuff 
     */
}

答案 1 :(得分:0)

布局文件的名称是什么?是item1.xml吗?如果是这种情况,您需要将其称为R.layout.item1

  

row = mInflater.inflate(R.layout.item1,parent,false);

您还在构造函数中创建了对inflater的引用,但是在getView方法中将其设置为null。使用mInflator并删除inflator = null