一个列表视图使用不同布局时的问题

问题描述 投票:0回答:1

基于一个list view上的查询,试图为不同类型的布局充气时,我遇到了问题。我使用这种类型,以便可以将整个Activity都包装到一个listView中,它看起来像一个可滚动的网页。我更喜欢AdapterView类型的布局,因为我必须在它们之间放置一些大表,如果我使用其他某种布局进行充气,则可能会延迟加载。

我在CursorAdapter类中使用了类似的东西

public View newView(Context arg0, Cursor cur, ViewGroup parent) {
            final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View view = null;
            int viewIndex = cur.getInt(1);
            if (viewIndex == 0)
               view = inflater.inflate(com.sayka.ordergadget.R.layout.takheader, parent, false);
            else if (viewIndex == 1)  {
                view = inflater.inflate(com.sayka.ordergadget.R.layout.activity_order_items2, parent, false);
                if (isNew) passFocus2Name((AutoCompleteTextView)view.findViewById(R.id.tak_itemName));
            }
            else if (viewIndex == 2)
                view = inflater.inflate(com.sayka.ordergadget.R.layout.tak_footer, parent, false);
            return view;
        }

问题在于,AdapterView似乎只会膨胀视图一次。因此,当我查询游标时,在BindView方法中捕获了[[空指针异常。 Cauz这次是适配器尝试从Layout2访问Layout1项,因为以前Layout2在Layout1那里。如果出现这样的错误,我试图更改视图本身

public void bindView(View view, Context cont, Cursor cur) { if (cur.getInt(1) == 2) { TextView totItems_L, totItems_C, totItems, totQty_L, totQty_C, totQty, totAmt_L, totAmt_C, totAmt; TextView testC; try { testC = (TextView)view.findViewById(com.sayka.ordergadget.R.id.totalItemsL); testC.setText("Salim"); } catch (NullPointerException exp) { ViewGroup parent = (ViewGroup)view.getParent(); final LayoutInflater inflater = LayoutInflater.from(parent.getContext()); view = inflater.inflate(com.sayka.ordergadget.R.layout.tak_footer, parent, false); }
这里我正在尝试重新分配视图。但是它不起作用。任何帮助,将不胜感激。我是android新手。
android android-listview nullpointerexception
1个回答
0
投票
在适配器中创建getItemViewType()就是为了这个确切目的。

@Override public int getViewTypeCount(){ return NUMBER_OF_TYPES; } @Override public int getItemViewType(int pos){ // get your item type // return type int, must be less than NUMBER_OF_TYPES }

http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)
© www.soinside.com 2019 - 2024. All rights reserved.