在listview中动态添加项目

时间:2014-10-05 10:08:33

标签: android listview adapter

我有一个适配器,可以从List<>中加载项目tempList。 首先,tempList有10个项目。当用户滚动到底部时,通过从另一个List属性详细信息列表中获取项目,自动将10个项目添加到tempList。

for (int i = 0; i < 10; i++) {
    tempList.add(propertyDetailsList.get(i));
}
propertyDetailsInstantAdapter.notifyDataSetChanged();

然而,结果是只有0(propertyDetailsList.get(0))的项被添加到tempList,并且它被添加10次而不是.... 我的代码怎么了?

更多代码:

private ListView propertyList;
private List<PropertyDetails> tempList;
private List<PropertyDetails> propertyDetailsList;
......
propertyDetailsList = getList();
tempList = propertyDetailsList.subList(0, 10);
......

public void showPropertyList() {
    propertyList.setAdapter(buildPropertyList());
    mIsLoading = false;
    propertyList.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (!mIsLoading && mMoreDataAvailable) {
                if (totalItemCount - visibleItemCount - AUTOLOAD_THRESHOLD <= firstVisibleItem) {
                    mIsLoading = true;
                    for (int i = 0; i < 10; i++) {
                    tempList.add(propertyDetailsList.get(10 + i));
                }
                    propertyDetailsInstantAdapter.notifyDataSetChanged();
                    mIsLoading = false;
                }
            }
        }
    });
}

public InstantAdapter<PropertyDetails> buildPropertyList() {
    propertyDetailsInstantAdapter = new InstantAdapter<PropertyDetails>(
            this, R.layout.property_list_item, PropertyDetails.class, tempList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PropertyListItem propertyListItem;
            Context context = parent.getContext();

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.property_list_item, parent, false);
                propertyListItem = new PropertyListItem();
                propertyListItem.propertyImage = (ImageView) convertView.findViewById(R.id.propertyListImage);
                propertyListItem.propertyAddress = (TextView) convertView.findViewById(R.id.propertyListAddress);
                propertyListItem.propertyName = (TextView) convertView.findViewById(R.id.propertyListName);
                propertyListItem.propertyDetails = this.getItem(position);
                convertView.setTag(propertyListItem);
            } else {
                propertyListItem = (PropertyListItem) convertView.getTag();
            }

            final PropertyDetails propertyDetails = this.getItem(position);
            Transformation transformation = new Transformation() {
                @Override
                public Bitmap transform(Bitmap source) {
                    int targetWidth = sp.getInt("deviceWidth", 0);
                    double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                    int targetHeight = (int) (targetWidth * aspectRatio);
                    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                    if (result != source) {
                        // Same bitmap is returned if sizes are the same
                        source.recycle();
                    }
                    return result;
                }

                @Override
                public String key() {
                    return "transformation" + " desiredWidth";
                }
            };
            if (propertyDetails.getImg_link() != null) {
                Picasso.with(context)
                        .load(Uri.parse(propertyDetails.getImg_link()))
                        .transform(transformation)
                        .into(propertyListItem.propertyImage);
            }
            propertyListItem.propertyAddress.setText(propertyDetails.getPropertyAddress());
            propertyListItem.propertyName.setText(propertyDetails.getPropertyName());

            return convertView;
        }
    };
    return propertyDetailsInstantAdapter;
}

0 个答案:

没有答案