如何使用自定义适配器

时间:2016-04-20 17:30:20

标签: android listview pull-to-refresh backendless

我想让列表刷新,以便我可以看到反映的变化

这是列表活动。这主要显示项目列表以及成本,数量等。每次刷新我想要更新的信息,如果在云中更改。

@Override
protected void onCreate( Bundle savedInstanceState )
{
    super.onCreate(savedInstanceState);
    setContentView( R.layout.content_cust_home);

    adapter = new ItemAdapter( ItemListingActivity.this, R.layout.activity_cust_home_display_list, totalItems );
    setListAdapter(adapter);

    QueryOptions queryOptions = new QueryOptions();


    BackendlessDataQuery query = new BackendlessDataQuery( queryOptions );

    Backendless.Data.of( Item_List.class ).find(query, new LoadingCallback<BackendlessCollection<Item_List>>(this,"Loading Items...", true) {
        @Override
        public void handleResponse(BackendlessCollection<Item_List> ItemsBackendlessCollection) {
            items = ItemsBackendlessCollection;

            addMoreItems(ItemsBackendlessCollection);

            super.handleResponse(ItemsBackendlessCollection);
        }
    });

    getListView().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 (needToLoadItems(firstVisibleItem, visibleItemCount, totalItemCount)) {
                isLoadingItems = true;

                items.nextPage(new LoadingCallback<BackendlessCollection<Item_List>>(ItemListingActivity.this) {
                    @Override
                    public void handleResponse(BackendlessCollection<Item_List> nextPage) {
                        items = nextPage;

                        addMoreItems(nextPage);

                        isLoadingItems = false;
                    }
                });
            }
        }
    });
}

/**
 * Determines whether is it needed to load more items as user scrolls down.
 *
 * @param firstVisibleItem number of the first item visible on screen
 * @param visibleItemCount number of items visible on screen
 * @param totalItemCount   total number of items in list
 * @return true if user is about to reach the end of a list, else false
 */
private boolean needToLoadItems( int firstVisibleItem, int visibleItemCount, int totalItemCount )
{
    return !isLoadingItems && totalItemCount != 0 && totalItemCount - (visibleItemCount + firstVisibleItem) < visibleItemCount / 2;
}

/**
 * Adds more items to list and notifies Android that dataset has changed.
 *
 * @param nextPage list of new items
 */
private void addMoreItems( BackendlessCollection<Item_List> nextPage )
{
    totalItems.addAll( nextPage.getCurrentPage() );
    adapter.notifyDataSetChanged();
}

@Override
protected void onListItemClick( ListView l, View v, int position, long id )
{
    Intent showItemIntent = new Intent( this, CustProductDetails.class );


   showItemIntent.putExtra("item",totalItems.get(position));
   startActivity(showItemIntent);
}

}

这是自定义适配器

public ItemAdapter( Context context, int resource, List<Item_List> items )
{
    super( context, resource, items );
    mResource = resource;
    mInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
}

/**
 * {@inheritDoc}
 */
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
    View view = convertView == null ? mInflater.inflate( mResource, parent, false ) : convertView;

    TextView ItemName = (TextView) view.findViewById(R.id.CustItemName);
    TextView ItemCost = (TextView) view.findViewById( R.id.CustItemCost );
    TextView ItemQuantity = (TextView) view.findViewById( R.id.CustItemQuantity );

    Item_List item = getItem( position );

    ItemName.setText(item.getItem_Name());
    ItemCost.setText( item.getProduct_cost().toString() );
    ItemQuantity.setText(item.getProduct_quantity());

    return view;
}

}

0 个答案:

没有答案