2013-07-08 64 views
1

我無法使用LayoutInflater對xml(包含QuickContactBadge的佈局)進行膨脹,以在ListView中使用它。它不會產生編譯/運行時錯誤或正確的預期輸出。在我從XML佈局文件中刪除QuickContactBadge後,它會正常膨脹。如何解決這個問題?如何使用Layout inflater在android中充氣快速聯繫徽章?

  1. 是否可以膨脹具有QuickContactBadge的XML佈局文件?
  2. 如果可能,膨脹一個具有QuickContactBadge的XML佈局文件在ListView中使用的正確過程是什麼?

編輯

message_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/lLayoutMessageItemHolder" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 


<QuickContactBadge 
    android:id="@+id/quickContactBadge1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<LinearLayout 
    android:id="@+id/lLayoutContentDisplayHolder" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tvPerson" 
     android:textIsSelectable="false" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/strPerson" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/tvMessage" 
     android:textIsSelectable="false" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/strLastMessage" /> 

    <TextView 
     android:id="@+id/tvTime" 
     android:textIsSelectable="false" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/strLastMessageReceivedTime" /> 

</LinearLayout> 

</LinearLayout> 

編輯:更新代碼 而getView()ContactListAdapter.java的方法

public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    //return super.getView(position, convertView, parent); 
    if(context != null){ 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View linearLayout = inflater.inflate(R.layout.message_item, null); 
     Contact contact = (Contact)getItem(position); 
     TextView tvPerson = (TextView)linearLayout.findViewById(R.id.tvPerson); 
     tvPerson.setText("Sample Text"); 
     Log.i("Test","Sample Text"); 
     return linearLayout; 
    }else{ 
     return null; 
    } 
} 

控制檯輸出:

07-10 17:00:26.511: I/Test(3574): Sample Text 
07-10 17:00:26.611: I/Test(3574): Sample Text 
07-10 17:00:26.621: I/Test(3574): Sample Text 
07-10 17:00:26.641: I/Test(3574): Sample Text 
07-10 17:00:26.661: I/Test(3574): Sample Text 
07-10 17:00:26.691: I/Test(3574): Sample Text 
07-10 17:00:26.701: I/Test(3574): Sample Text 

P.S:沒有爲該佈局中使用的所有小部件設置值。我期望列表中只有人名,但在編譯和運行時不會產生任何錯誤。但它只顯示空屏幕。

+0

發佈您使用的確切代碼,您使用的xml以及生成的確切錯誤。 –

+0

抱歉Sechan延遲,現在我更新了問題。謝謝你的時間。 –

回答

1

你的問題是在你的getView函數中 - 它需要返回你膨脹的linearLayout。我不確定它的返回視圖不是局部變量。

此外,如果convertView傳入的值爲null,則只需要充氣 - 否則,您需要設置數據而不膨脹以重用視圖。

+0

哦對不起抱歉,現在我得到了我犯的錯誤。它只是一個空視圖。非常感謝。我會嘗試這個更正的代碼並很快發佈。再次非常感謝你。 –

+0

嗨Sechan,我已經更新了你所說的代碼,但現在我面臨着另一個問題。現在充氣佈局顯示(但只有一個,即使列表有7個聯繫人,我也更新了控制檯輸出)。 –