Placeslistadapter.java
public class Placeslistadapter extends ArrayAdapter<Place> implements Filterable { public Context context; private List<Place> places,orig,itemDetailsrrayList; private PlaceFilter filter; public Placeslistadapter(Context context,int textVIEwResourceID) { super(context,textVIEwResourceID); } public Placeslistadapter(Context context,int resource,List<Place> places) { super(context,resource,places); this.context = context; this.places = places; itemDetailsrrayList = places; orig = new ArrayList<Place>(itemDetailsrrayList); filter = new PlaceFilter(); // imageLoader = new ImageLoader(context.getApplicationContext()); } public int getCount() { return itemDetailsrrayList.size(); } public Place getItem(int position) { return itemDetailsrrayList.get(position); } public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEw vIEw = convertVIEw; if (vIEw == null) { LayoutInflater inflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); vIEw = inflater.inflate(R.layout.List_item_place,null); } Place place = places.get(position); if (place != null) { TextVIEw place_name = (TextVIEw) vIEw .findVIEwByID(R.ID.place_Title); TextVIEw place_distance = (TextVIEw) vIEw .findVIEwByID(R.ID.place_distance); ImageVIEw place_category_icon = (ImageVIEw) vIEw .findVIEwByID(R.ID.place_category_icon); if (place_name != null) { place_name.setText(place.getPlaceTitle()); } if (place_distance != null) { place_distance.setText("198"); } if (place_category_icon != null) { place_category_icon.setimageResource(R.drawable.icon_category); } } // Setting Alternative Row colors if (position % 2 == 0) { vIEw.setBackgroundResource(R.drawable.List_vIEw_place_row_1); } else { vIEw.setBackgroundResource(R.drawable.List_vIEw_place_row_2); } return vIEw; } @OverrIDe public Filter getFilter() { // Todo auto-generated method stub return filter; } private class PlaceFilter extends Filter { @OverrIDe protected FilterResults performFiltering(CharSequence constraint) { FilterResults oReturn = new FilterResults(); ArrayList<Place> results = new ArrayList<Place>(); if (orig == null) orig = itemDetailsrrayList; if (constraint != null) { if (orig != null && orig.size() > 0) { for (Place g : orig) { if (g.getPlaceTitle() .tolowerCase() .startsWith(constraint.toString().tolowerCase())) results.add(g); } } oReturn.values = results; } return oReturn; } @SuppressWarnings("unchecked") @OverrIDe protected voID publishResults(CharSequence constraint,FilterResults results) { itemDetailsrrayList = (ArrayList<Place>) results.values; notifyDataSetChanged(); } }}
Place.java
public class Place { Integer placeID; String placename = "",placedistance = "",placecategoryIcon = ""; public Place(int placeID,String placename,String placedistance,String placecategoryIcon) { this.placeID = placeID; this.placename = placename; this.placedistance = placedistance; this.placecategoryIcon = placecategoryIcon; } public Integer getPlaceID() { return placeID; } public voID setPlaceID(int placeID) { this.placeID = placeID; } public String getPlacename() { return placename; } public voID setPlacename(String placename) { this.placename = placename; } public String getPlacedistance() { return placedistance; } public voID setPlacedistance(String placedistance) { this.placedistance = placedistance; } public String getPlacecategoryIcon() { return placecategoryIcon; } public voID setPlacecategoryIcon(String placecategoryIcon) { this.placecategoryIcon = placecategoryIcon; }}
主要活动
protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getwindow().requestFeature(Window.FEATURE_ACTION_bar); setContentVIEw(R.layout.activity_main); context = this; Log.i("Nomad","onCreate"); List<Place> thePlaces = new ArrayList<Place>(); for (int i = 0; i < places.length; i++) { Place pl = new Place(i,places[i],"NO_disTANCE","NO_categoryICON"); thePlaces.add(pl); } ListVIEw = (ListVIEw) findVIEwByID(R.ID.place_List); ListVIEw.setEmptyVIEw(findVIEwByID(R.ID.empty)); adapter = new Placeslistadapter(MainActivity.this,thePlaces); ListVIEw.setAdapter(adapter); ListVIEw.setTextFilterEnabled(true); mSearchVIEw = (SearchVIEw) findVIEwByID(R.ID.action_search); ListVIEw.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> a,VIEw vIEw,int position,long ID) { startActivity(new Intent(MainActivity.this,PlaceActivity.class)); } }); }
activity_main.xml中
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="@color/primary_white" > <ListVIEw androID:ID="@+ID/place_List" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:cachecolorHint="#00000000" androID:fadingEdge="none" > </ListVIEw> <TextVIEw androID:ID="@+ID/empty" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:padding="20dp" androID:text="@string/List_vIEw_place_empty" androID:textcolor="@color/black" androID:textSize="18sp" /></linearLayout>
List_item_place.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:paddingBottom="10dp" androID:paddingtop="10dp" > <ImageVIEw androID:ID="@+ID/place_category_icon" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:contentDescription="ss" androID:paddingleft="10dp" androID:paddingRight="15dp" androID:src="@drawable/icon_category" /> <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:paddingRight="15dp" androID:textcolor="@color/black" androID:text="320" /> <TextVIEw androID:ID="@+ID/place_Title" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:layout_toRightOf="@+ID/place_category_icon" androID:ellipsize="end" androID:paddingRight="50dp" androID:singleline="true" androID:text="Place name" androID:textcolor="#191919" androID:textSize="18sp" /></relativeLayout>
这就是它在Load上的显示方式
在尝试滚动内容时重新出现.
解决方法 你必须删除getwindow().requestFeature(Window.FEATURE_ACTION_bar);
Window.FEATURE_ACTION_BAR已添加到Honeycomb(3.0)中,不适用于AndroID 2.x设备.
要与旧版本保持兼容,您可以在MainActivity.java中使用此代码段:
int currentAPIVersion = androID.os.Build.VERSION.SDK_INT;if (currentAPIVersion >= 11) { getwindow().requestFeature(Window.FEATURE_ACTION_bar);}
附加代码:(这些是上面示例中的派生类,由于缺少类,必须更改某些元素,注释或以某种方式 *** 作以进行编译)
MainActivity.java
在MainActivity中,我做的唯一真正的改变是禁用小于11的API级别的Actionbar.并且在插入位置后也启动notifyDatasetchanged().
public class MainActivity extends Activity { protected static final String TAG = "MainActivity"; private MainActivity context; private ListVIEw ListVIEw; private Placeslistadapter adapter; protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG,"onCreate()"); int currentAPIVersion = androID.os.Build.VERSION.SDK_INT; if (currentAPIVersion >= 11) { getwindow().requestFeature(Window.FEATURE_ACTION_bar); } setContentVIEw(R.layout.activity_main); context = this; Log.i("Nomad","onCreate"); List<Place> thePlaces = new ArrayList<Place>(); String[] places = new String[10]; places[0] = "hallo1"; places[1] = "hallo2"; places[2] = "hallo3"; places[3] = "hallo4"; places[4] = "hallo5"; places[5] = "hallo6"; places[6] = "hallo7"; places[7] = "hallo8"; places[8] = "hallo9"; places[9] = "hallo10"; for (int i = 0; i < places.length; i++) { Place pl = new Place(i,"NO_categoryICON"); thePlaces.add(pl); } ListVIEw = (ListVIEw) findVIEwByID(R.ID.place_List); ListVIEw.setEmptyVIEw(findVIEwByID(R.ID.empty)); adapter = new Placeslistadapter(this,R.layout.List_item_place,thePlaces); ListVIEw.setTextFilterEnabled(true); // mSearchVIEw = (SearchVIEw) findVIEwByID(R.ID.action_search); ListVIEw.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> a,MainActivity.class)); } }); ListVIEw.setAdapter(adapter); **adapter.notifyDataSetChanged();** }}
Placeslistadapter.java
对于适配器,我强烈重新使用VIEwHolder,它包含每个List-Element-VIEw的TextVIEws和ImageVIEw引用.
public class Placeslistadapter extends ArrayAdapter<Place> implements Filterable { private static final String TAG = "Placeslistadapter"; public Context context; private List<Place> places,places); this.context = context; this.places = places; itemDetailsrrayList = places; orig = new ArrayList<Place>(itemDetailsrrayList); filter = new PlaceFilter(); // imageLoader = new ImageLoader(context.getApplicationContext()); } public int getCount() { return itemDetailsrrayList.size(); } public Place getItem(int position) { return itemDetailsrrayList.get(position); } public long getItemID(int position) { return position; } /** * This is the holder that will provIDe fast access to arbitrary objects and * vIEws. Use a subclass to adapt it for your needs. */ public static class VIEwHolder { private final TextVIEw place_name; private final TextVIEw place_distance; private final ImageVIEw place_category_icon; public VIEwHolder(TextVIEw place_name,TextVIEw place_distance,ImageVIEw place_category_icon) { this.place_name = place_name; this.place_distance = place_distance; this.place_category_icon = place_category_icon; } } protected VIEwHolder createHolder(VIEw v) { TextVIEw place_name = (TextVIEw) v.findVIEwByID(R.ID.place_Title); TextVIEw place_distance = (TextVIEw) v .findVIEwByID(R.ID.place_distance); ImageVIEw place_category_icon = (ImageVIEw) v .findVIEwByID(R.ID.place_category_icon); VIEwHolder tvshowHolder = new VIEwHolder(place_name,place_distance,place_category_icon); return tvshowHolder; } @OverrIDe public VIEw getVIEw(int position,VIEwGroup parent) { VIEw vIEw = convertVIEw; VIEwHolder holder; if (vIEw == null) { LayoutInflater inflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); vIEw = inflater.inflate(R.layout.List_item_place,null); // Log.v(TAG,"generating new vIEw"); holder = createHolder(vIEw); vIEw.setTag(holder); } else { holder = (VIEwHolder) vIEw.getTag(); } Place place = places.get(position); if (place != null) { // Log.v(TAG,place.getPlacename()); if (holder.place_name != null) { // Log.v(TAG,"setting placename to " + place.getPlacename()); // place_name.setText(place.getPlaceTitle()); holder.place_name.setText(place.getPlacename()); } if (holder.place_distance != null) { holder.place_distance.setText("198"); } if (holder.place_category_icon != null) { holder.place_category_icon .setimageResource(R.drawable.ic_launcher); } } // Setting Alternative Row colors if (position % 2 == 0) { vIEw.setBackgroundResource(R.drawable.ic_launcher); } else { vIEw.setBackgroundResource(R.drawable.ic_launcher); } return vIEw; } @OverrIDe public Filter getFilter() { // Todo auto-generated method stub return filter; } private class PlaceFilter extends Filter { @OverrIDe protected FilterResults performFiltering(CharSequence constraint) { FilterResults oReturn = new FilterResults(); ArrayList<Place> results = new ArrayList<Place>(); if (orig == null) orig = itemDetailsrrayList; if (constraint != null) { if (orig != null && orig.size() > 0) { for (Place g : orig) { if (g.getPlaceTitle() .tolowerCase() .startsWith(constraint.toString().tolowerCase())) results.add(g); } } oReturn.values = results; } return oReturn; } @SuppressWarnings("unchecked") @OverrIDe protected voID publishResults(CharSequence constraint,FilterResults results) { itemDetailsrrayList = (ArrayList<Place>) results.values; notifyDataSetChanged(); } }}总结
以上是内存溢出为你收集整理的Android 2.X中的Android ListView问题全部内容,希望文章能够帮你解决Android 2.X中的Android ListView问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)