android - 如何使用 Layout inflater 在 android 中为 Quick contact badge 充气?

标签 android android-listview layout-inflater xml-layout

我无法使用 LayoutInflater 扩充 xml(包含 QuickContactBadge 的布局)文件,以便在 ListView 中使用它。它不会产生编译/运行时错误或正确的预期输出。从 XML 布局文件中删除 QuickContactBadge 后,它会正确膨胀。如何解决这个问题?

  1. 是否可以扩充具有 QuickContactBadge 的 XML 布局文件?
  2. 如果可能,扩充具有 QuickContactBadge 的 XML 布局文件以在 ListView 内部使用的正确程序是什么?

编辑

message_item.xml

<pre><code><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> </code></pre>

编辑:更新代码 和 ContactListAdapter.java 的 getView() 方法

<pre><code>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; } } </code></pre>

控制台输出:

<pre><code>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 </code></pre>

P.S:我没有为该布局中使用的所有小部件设置值。我希望列表只有人名,但它在编译和运行时都没有产生任何错误。但它只显示空屏幕。

最佳答案

您的问题出在您的 getView 函数中 - 它需要返回您膨胀的 linearLayout。我不确定它返回的是什么—— View 不是局部变量。

此外,如果传入的 convertView 为 null,您只想膨胀 - 否则您想要设置数据而不膨胀以重用 View 。

关于android - 如何使用 Layout inflater 在 android 中为 Quick contact badge 充气?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17529998/

相关文章:

当系统自动配对/连接蓝牙设备时,Android 会收到回调吗?

android - 如何使用通用图像加载器

android - ListView setSelection 不适用于选择简单列表

带有自定义适配器的 android listview 卡住 Activity

javascript - Cordova : How to enable viewport zoom

java - Android 什么是命名也执行逻辑的 boolean 函数的最佳方式?

android - 如何在单击按钮时膨胀另一个 xml

android - 复选框会在 Android 中自动选中

android - inflatedView 的 getId() 返回什么?

android - Gradle 错误。混合库必须为相同版本