具有不同的布局的ListView行 [英] ListView rows with different layouts

查看:103
本文介绍了具有不同的布局的ListView行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到的,是有不同的布局每一行。在code在我的 ArrayAdapter 如下:

What I am trying to achieve is to have different layouts for each row. The code in my ArrayAdapter is as follows:

@Override
    public int getCount() {
        return contentList.size();
    }

    @Override
    public MyContents getItem(int position) {
        return content.get(position);
    }

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

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyContents entry = content.get(position);

        View row = convertView;
        LayoutInflater inflater = null;
        boolean _haschild_ = entry.testBoolean();
        if (row == null) {
            if (entry.testBoolean()) {
                inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater
                        .inflate(R.layout.contents_layout, null);
            } else {
                inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.test_layout, null);
            }
        } 

        if (entry.testBoolean()) {
            TextView txtView = (TextView) row
                    .findViewById(R.id.txtView);
            //.....
        } else {
            ((TextView) row.findViewById(R.id.txtView_test)) //**LOE**
                    .setText("test: " + position); //**LOE**
        }


        return row;
    }

在布局文件* t​​est_layout.xml *:

The layout file for *test_layout.xml*:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:background="@color/grey_five"
    android:orientation="vertical"
    android:padding="@dimen/padding_small" >

    <TextView
        android:id="@+id/txtView_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/lorem__ipsum"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/grey" />


</RelativeLayout>

但每当我执行它抛出一个 NullPointerException异常(检查与注释行// LOE )。

But whenever I execute it throws a NullPointerException(check the line with the comment //LOE).

任何建议怎么办?我挣扎与它过去的一天。

Any suggestion what to do? I am struggling with it for past one day.

推荐答案

NullPointerException异常是因为你没有执行 getItemViewType 。现在您返回的行布局的第一种类型,不管是什么,这是不具备* txtView_test * 布局的TextView )。您的code应该是这样的:

That NullPointerException occurs because you didn't implement the getItemViewType. Right now you return the first type of row layout no matter what and this is a layout that doesn't have the *txtView_test* TextView). Your code should be something like this:

// some fields
public static final int FIRST_TYPE = 0;
public static final int SECOND_TYPE = 1;

@Override
public int getItemViewType(int position) {
     MyContents item = getItem(position);
     if (item.testBoolean()) {
          return FIRST_TYPE;
     } else {
          return SECOND_TYPE; 
     } 
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final MyContents entry = content.get(position);
    View row = convertView;
    LayoutInflater inflater = null;
    int type = getItemViewType(position);
    boolean _haschild_ = entry.testBoolean();
    if (row == null) {
        if (type == FIRST_TYPE) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater
                    .inflate(R.layout.contents_layout, null);
        } else {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.test_layout, null);
        }
    } 
    if (type == FIRST_TYPE) {
        TextView txtView = (TextView) row
                .findViewById(R.id.txtView);
        //.....
    } else {
        ((TextView) row.findViewById(R.id.txtView_test)) //**LOE**
                .setText("test: " + position); //**LOE**
    }

这篇关于具有不同的布局的ListView行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆