Android 适配器未正确显示布局
好的,我已经创建了我的自定义ArrayAdapter,并定义了一个list_item.xml文件,从理论上讲,该文件应该生成ListView的每一行,在左边有一张图片,图片旁边有两行(一行有联系人姓名,一行有联系人电话号码),我目前只在网上显示带有联系人电话号码的图片。以下是我的ListAdapter代码和list_item.xml代码: 列表适配器Android 适配器未正确显示布局,android,xml,adapter,Android,Xml,Adapter,好的,我已经创建了我的自定义ArrayAdapter,并定义了一个list_item.xml文件,从理论上讲,该文件应该生成ListView的每一行,在左边有一张图片,图片旁边有两行(一行有联系人姓名,一行有联系人电话号码),我目前只在网上显示带有联系人电话号码的图片。以下是我的ListAdapter代码和list_item.xml代码: 列表适配器 package com.example.codingchallenge; import java.util.ArrayList; import
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;
}
}
package com.example.codingchallenge;
导入java.util.ArrayList;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类ListAdapter扩展了ArrayAdapter{
私人ArrayList联系人;
私有数组列表数;
私人最终语境;
私有数组列表位图;
公共ListAdapter(上下文上下文、ArrayList位图、ArrayList联系人){
//呼叫超级
超级(上下文、右布局、列表项、联系人);
//分配变量
this.context=上下文;
this.bitmap=位图;
这个。联系人=联系人;
数字=新的ArrayList();
//从contacts ArrayList中抓取PhoneNumber对象
用于(联系人信息联系人:联系人){
add(contact.getPhone());
}
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
//为适配器的布局充气
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.list_项,父项,false);
//实例化不同的视图
ImageView image=(ImageView)view.findViewById(R.id.缩略图);
TextView name=(TextView)view.findViewById(R.id.name);
TextView电话号码=(TextView)view.findViewById(R.id.phoneNumber);
//根据需要设置图像和文本
image.setImageBitmap(bitmap.get(position));
name.setText(contacts.get(position.getName());
setText(numbers.get(position.getWork());
返回视图;
}
}
和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>
知道为什么我的版面中没有显示名称行吗?试试这个
<?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>
将TextView和LinearLayout放在LinearLayout中,并将此属性赋予LinearLayout android:layout\u toRightOf=“@id/缩略图”。它会解决你的问题 试试这个布局
在我的情况下非常适合:
<?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>
试试这个布局
<?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>
使用此选项
<?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>
addandroid:layout_lower=“@id/name”
在您的PhonenumberTextView
中,它抛出一个错误,指出循环布局不能存在于相对布局中,我假设我已经将TextView的名称定义为低于PhoneNumber 1。这就是我刚才尝试的,并得到一个错误,即在相对布局中不能存在圆形布局。这仍然在底部显示TextView的名称。我认为适配器现在有其他问题。这是名称TextView第三次出现在底部。将其全部放在RelativeLayout中,然后只嵌套一个LinearLayout?或者我应该将整个布局更改为线性,并将ImageView移到该布局之外?保留相对布局,并将LinearLayout放在其中。嵌套布局。这很有效,但出于某种原因,phoneNumber TextView现在位于名称TextView的顶部。@Bill但在我的例子中,它在顶部显示名称TextView,并且很完美。您是否使用与我相同的ListAdapter代码?“我不知道我这方面出了什么问题。”比尔:你也一样。这非常奇怪。这非常奇怪,我想知道在另一段代码中是否有什么地方出错了,尽管我不可能想到是什么。