适配器未正确显示布局

时间:2014-03-07 04:19:53

标签: android xml adapter

好的,所以我创建了自定义ArrayAdapter,并定义了一个list_item.xml文件,理论上应该生成我的ListView的每一行,在左边有一张图片,图片旁边有两行(一行包含联系人姓名,以及联系电话号码。)但是,我目前只显示带有联系电话号码的图片。这是我的ListAdapter代码和list_item.xml代码:

ListAdapter

package com.example.codingchallenge;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends ArrayAdapter<ContactInfo>{

private ArrayList<ContactInfo> contacts;
private ArrayList<PhoneNumber> numbers;
private final Context context;
private ArrayList<Bitmap> bitmap;

public ListAdapter(Context context, ArrayList<Bitmap> bitmaps, ArrayList<ContactInfo> contacts) {
    //Call to Super
    super(context, R.layout.list_item, contacts);

    //Assign variables
    this.context = context;
    this.bitmap = bitmaps;
    this.contacts = contacts;
    numbers = new ArrayList<PhoneNumber>();

    // Grab the PhoneNumber objects out of the contacts ArrayList
    for(ContactInfo contact: contacts) {
        numbers.add(contact.getPhone());
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    //Inflate the layout for our Adapter
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_item, parent, false);

    //Instantiate different views
    ImageView image = (ImageView) view.findViewById(R.id.thumbnail);
    TextView name = (TextView) view.findViewById(R.id.name);
    TextView phoneNumber = (TextView) view.findViewById(R.id.phoneNumber);

    //Set the image and text as what we want
    image.setImageBitmap(bitmap.get(position));
    name.setText(contacts.get(position).getName());
    phoneNumber.setText(numbers.get(position).getWork());

    return view;
}

}

和list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/phoneNumber"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/thumbnail"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" 
    android:ellipsize="end"/>

<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/phoneNumber"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/thumbnail"
    android:gravity="center_vertical"
    android:text="Example application"
    android:layout_alignWithParentIfMissing="true"
    android:textSize="16sp" 
    android:ellipsize="end"/>

</RelativeLayout>

知道为什么名称行没有显示在我的布局中?

5 个答案:

答案 0 :(得分:1)

尝试这个Layout它完全适合我的情况:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/thumbnail"
    android:ellipsize="end"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp" />

<TextView
    android:id="@+id/phoneNumber"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_below="@id/name"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/thumbnail"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" />

</RelativeLayout>

答案 1 :(得分:1)

使用此功能。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dip"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/phoneNumber"
    android:layout_toRightOf="@+id/thumbnail"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp" />

<TextView
    android:id="@+id/phoneNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/name"
    android:layout_alignParentTop="true"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" />

</RelativeLayout>

答案 2 :(得分:0)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/thumbnail"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/phoneNumber"
        android:layout_width="fill_parent"
        android:layout_height="26dip"           
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>

答案 3 :(得分:0)

将TextView放在LinearLayout中并为LinearLayout提供此属性android:layout_toRightOf =“@ id / thumbnail”。它会解决你的问题。

答案 4 :(得分:0)

试试这个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/phoneNumber"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/thumbnail"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/phoneNumber"
        android:layout_toRightOf="@id/thumbnail"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />

</RelativeLayout>