【问题标题】:How to inflate Quick contact badge in android using Layout inflater?如何使用 Layout inflater 在 android 中为快速联系人徽章充气?
【发布时间】:2013-07-08 15:04:49
【问题描述】:

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

  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>

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

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,现在我更新了问题。感谢您的宝贵时间。

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


【解决方案1】:

您的问题在于您的 getView 函数 - 它需要返回您膨胀的线性布局。我不确定它的返回视图不是局部变量。

此外,您只想在传入的 convertView 为 null 时进行膨胀 - 否则您想设置数据而不进行膨胀以重用视图。

【讨论】:

  • 哦,对不起,对不起,现在我知道我犯了什么错误。它只不过是一个空视图。非常感谢。我会尝试这个更正的代码并很快发布给你。再次非常感谢您。
  • 嗨 Sechan,我已经按照你的说法更新了代码,但现在我面临另一个问题。现在正在显示膨胀的布局(但只有一个,即使列表有 7 个联系人,我也更新了控制台输出)。
猜你喜欢
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
相关资源
最近更新 更多