【发布时间】:2015-01-20 00:50:31
【问题描述】:
我正在尝试使用自定义适配器做一个列表,但是在我调用 inflate 时的代码部分中,我得到了OutOfResourcesException,我进行了几次测试但我没有发现原因
当调试执行此行时:rowView = inflater.inflate(R.layout.list_chat, parent, false);
然后调用异常。
public class ListChatAdapter extends BaseAdapter{
public class Contact {
private String name;
private String sentence;
private Bitmap img;
Contact(String name, String sentence, Bitmap bmp) {
this.name = name;
this.sentence = sentence;
this.img = bmp;
}
void setName(String name) {
this.name = name;
}
void setSentence(String sentence) {
this.sentence = sentence;
}
void setBmp(Bitmap bmp) {
this.img = bmp;
}
String getName() {
return this.name;
}
String getSentence() {
return this.sentence;
}
Bitmap getImg() {
return this.img;
}
}
private static class ViewHolderItem {
TextView textName;
TextView textSentence;
ImageView img;
}
private static int count = 0;
private static List<Contact> contacts = new ArrayList<Contact>();
private Context context;
public ListChatAdapter(Context c) {
context = c;
}
@Override
public int getCount() {
return count;
}
public void addItem(Contact contact) {
contacts.add(contact);
count++;
notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
View rowView = convertView;
if(rowView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
// Exception here
rowView = inflater.inflate(R.layout.list_chat, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.textName = (TextView) convertView
.findViewById(R.id.text_name);
viewHolder.textSentence = (TextView) convertView
.findViewById(R.id.text_sentence);
viewHolder.img = (ImageView) convertView.findViewById(R.id.img_chat);
// store the holder with the view.
rowView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) rowView.getTag();
}
// object item based on the position
Contact objectItem = contacts.get(position);
// assign values if the object is not null
if(objectItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.textName.setText(objectItem.getName());
viewHolder.textSentence.setText(objectItem.getSentence());
viewHolder.img.setImageBitmap(objectItem.getImg());
}
return rowView;
}
}
以及布局调用list_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation = "vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="4dp"
android:paddingRight="4dp" >
<ImageView
android:id="@+id/img_chat"
android:layout_width="55dp"
android:layout_height="65dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text_name"
android:layout_alignRight="@id/img_chat"
android:layout_alignEnd="@id/img_chat"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/text_sentence"
android:layout_alignRight="@id/img_chat"
android:layout_alignEnd="@id/img_chat"
android:layout_below="@id/text_sentence" />
</RelativeLayout>
</Linearlayout>
【问题讨论】:
标签: android listview adapter layout-inflater