我無法使用LayoutInflater對xml(包含QuickContactBadge的佈局)進行膨脹,以在ListView中使用它。它不會產生編譯/運行時錯誤或正確的預期輸出。在我從XML佈局文件中刪除QuickContactBadge後,它會正常膨脹。如何解決這個問題?如何使用Layout inflater在android中充氣快速聯繫徽章?
- 是否可以膨脹具有QuickContactBadge的XML佈局文件?
- 如果可能,膨脹一個具有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:沒有爲該佈局中使用的所有小部件設置值。我期望列表中只有人名,但在編譯和運行時不會產生任何錯誤。但它只顯示空屏幕。
發佈您使用的確切代碼,您使用的xml以及生成的確切錯誤。 –
抱歉Sechan延遲,現在我更新了問題。謝謝你的時間。 –