Android 2.X中的Android ListView问题

Android 2.X中的Android ListView问题,android,android-listview,actionbarsherlock,android-arrayadapter,custom-adapter,Android,Android Listview,Actionbarsherlock,Android Arrayadapter,Custom Adapter,我已经创建了一个自定义数组适配器来填充列表视图,当活动加载时,列表视图文本将消失 PlacesListAdapter.java public class PlacesListAdapter extends ArrayAdapter<Place> implements Filterable { public Context context; private List<Place> places, orig, itemDetailsrrayLi

我已经创建了一个自定义数组适配器来填充列表视图,当活动加载时,列表视图文本将消失

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();
        }

    }
}
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;
    }

}
main活动

    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));
            }
        });
    }
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE\u ACTION\u栏);
setContentView(R.layout.activity_main);
上下文=这个;
Log.i(“Nomad”、“onCreate”);
列出places=new ArrayList();
for(int i=0;i
活动\u 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>
<?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>

列出项目位置.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>
<?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>

这是加载时的显示方式

尝试滚动时,内容将重新出现。 您必须删除

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
已在蜂巢(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, 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(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, View view, int position,
                    long id) {

                startActivity(new Intent(MainActivity.this, MainActivity.class));
            }
        });
        listView.setAdapter(adapter);
        **adapter.notifyDataSetChanged();**
    }
}
公共类MainActivity扩展活动{
受保护的静态最终字符串TAG=“MainActivity”;
私人活动语境;
私有列表视图列表视图;
私人场所适配器;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.v(标记“onCreate()”);
int currentapiVersion=android.os.Build.VERSION.SDK\u int;
如果(当前版本>=11){
getWindow().requestFeature(Window.FEATURE\u ACTION\u栏);
}
setContentView(R.layout.activity_main);
上下文=这个;
Log.i(“Nomad”、“onCreate”);
列出places=new ArrayList();
字符串[]位置=新字符串[10];
地点[0]=“你好1”;
地点[1]=“hallo2”;
地点[2]=“hallo3”;
地点[3]=“hallo4”;
地点[4]=“hallo5”;
地点[5]=“hallo6”;
地点[6]=“hallo7”;
地点[7]=“hallo8”;
地点[8]=“hallo9”;
地点[9]=“hallo10”;
for(int i=0;i
PlacesListAdapter.java

对于适配器,我强烈建议使用ViewHolder,它包含每个列表元素视图的TextView和ImageView引用

public class PlacesListAdapter extends ArrayAdapter<Place> implements
        Filterable {
    private static final String TAG = "PlacesListAdapter";
    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;
    }

    /**
     * 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, View convertView, 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();
        }

    }
}
公共类PlacesListAdapter扩展了ArrayAdapter实现
可过滤{
私有静态最终字符串标记=“PlacesListAdapter”;
公共语境;
私人列表地点、来源、项目详细信息列表;
私募基金;
public PlacesListAdapter(上下文,int textViewResourceId){
super(上下文,textViewResourceId);
}
public PlacessListAdapter(上下文上下文、int资源、列表位置){
超级(上下文、资源、地点);
this.context=上下文;
这个地方=地方;
ItemDetailsRayList=地点;
orig=新阵列列表(ItemDetailsRaylist);
filter=新的PlaceFilter();
//imageLoader=新的imageLoader(context.getApplicationContext());
}
public int getCount(){
return itemtailsrraylist.size();
}
公共场所getItem(内部位置){
返回ItemDetailsRayList.get(位置);
}
公共长getItemId(int位置){
返回位置;
}
/**
*这是一个可以快速访问任意对象和对象的支架
*视图。使用子类使其适应您的需要。
*/
公共静态类视图持有者{
私有最终文本视图地点名称;
私人最终文本视图位置\距离;
私有最终图像视图位置\类别\图标;
公共视图持有者(文本视图地点名称、文本视图地点距离、,
图像视图位置(类别图标){
this.place\u name=place\u name;
this.place\u distance=place\u distance;
this.place\u category\u icon=place\u category\u icon;
}
}