如何使用Layout inflater在android中充气快速联系人徽章?

时间:2013-07-08 15:04:50

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

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

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

    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>
    

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

    控制台输出:

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

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

1 个答案:

答案 0 :(得分:1)

你的问题出在你的getView函数中 - 它需要返回你膨胀的linearLayout。我不确定它的返回视图是不是局部变量。

此外,如果传入的convertView为null,您只想要膨胀 - 否则您希望在不膨胀的情况下设置数据以重用视图。

相关问题