我有一个自定义数组适配器,我想在 ListView 中有一个图像视图。这只发生在 2.2 和 2.3.X 设备上。
我的行模板如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/place_category_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"
android:background="@drawable/photo1"
android:scaleType="centerCrop" >
</ImageView>
<TextView
android:id="@+id/place_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/list_timestamp"
android:ellipsize="end"
android:singleLine="true"
android:text="Harsha Mallikarjun Vantagudi"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="6dp"
android:paddingLeft="30dp"
android:singleLine="true"
android:text="2 hours" >
</TextView>
</LinearLayout>
当我加载列表时。图像已显示,但当我尝试滚动文本时文本消失,文本随机重新出现并消失。
适配器
public class PlacesListAdapter extends ArrayAdapter<Place> implements
Filterable {
public Context context;
private List<Place> places;
public PlacesListAdapter(Context context, List<Place> places) {
super(context, R.layout.list_item_place, places);
this.context = context;
this.places = places;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater;
inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.list_item_place, null);
}
Place place = places.get(position);
if (place != null) {
TextView place_title = (TextView) view
.findViewById(R.id.place_name);
TextView place_distance = (TextView) view
.findViewById(R.id.place_distance);
if (place_title != null) {
place_title.setText(place.getPlaceName());
}
if (place_distance != null) {
place_distance.setText(place.getPlaceDistance());
}
}
return view;
}
}