2015-04-04 89 views
1

如何在一個適配器中使用兩個不同的視圖?我需要一個佈局中的3個字段和其他4個字段。在一個適配器中的多個視圖(充氣器)

此代碼工作正常,如果我在屏幕只有1-5項(擴展底座適配器):

@Override 
public View getView (final int position, View convertView, ViewGroup parent) { 
FieldsAndText p = getFields(position); 

view = convertView; 
    if (position != 3) { 
     if (view == null) { 
     view = lInflater.inflate(R.layout.addproduct_item, parent, false); 
     } 
    ((TextView) view.findViewById(R.id.addProductTextView)).setText(p.name); 
    EditText editText = (EditText) view.findViewById(R.id.addProductEditText); 
    editText.setText(p.value); 
    } 
    else if (position == 3) { 
     if (view == null) { 
     view = lInflater.inflate(R.layout.alternative_item, parent, false); 
     TextView textView = (TextView) view.findViewById(R.id.alternative_textView1); 
     } 
    } 
    return view; 
} 

但是,當有更多然後5個項目,我做向下滾動,我有一個錯誤:

Null pointer exception in: ((TextView) view.findViewById(R.id.addProductTextView)).setText(p.name);

+0

https://edisonthk.wordpress.com/2014/06/12/constraints-of-multiple-layouts-listview-for-android/它會幫助你 – 2015-04-04 16:46:25

回答

0

您需要實現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); 
    } 
    } 
} 

getViewTypeCount()回報2,因爲我有兩種類型的行。 getItemViewType()對於標題行返回0,對於詳細行,返回1,基於具有Integer作爲標題的模型數據的getItem()方法的結果,以及從我的數組陣列作爲String作爲詳細信息行的模型數據。

相關問題