2012-07-25 114 views
2

我試圖實現的是爲每一行設置不同的佈局。在我ArrayAdapter的代碼如下:具有不同佈局的ListView行

@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; 
    } 

的佈局文件* 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)。

任何建議該怎麼辦?過去一天我一直在努力。

回答

4

NullPointerException是因爲你沒有實現getItemViewType。現在,無論什麼情況,您都可以返回第一種行佈局,這是一個沒有* txtView_test * TextView)的佈局。你的代碼應該是這樣的:

// 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** 
    } 
+0

這我什麼,我終於實現.... – Debopam 2012-07-25 08:20:55

0

使用,

LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View row = inflater.inflate(R.layout.test_layout, parent, false); 

可以將它解決您的問題。

+0

它不工作.... – Debopam 2012-07-25 08:14:10

0

嘗試這個:::

convertView = getLayoutInflater().inflate(R.layout.my_row_layout, null); 


TextView txtName = (TextView)convertView.findViewById(R.id.txtMyTextView); 

txtName.setText("Hi,,,,. Hows You..."); 
+0

「返回convertView」 – 2012-07-25 08:10:57

相關問題