Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Displaying Progress with Custom Adapter
/**
* A child class shall subclass this Adapter and
* implement method getDataRow(int position, View convertView, ViewGroup parent),
* which supplies a View present data in a ListRow.
*
* This parent Adapter takes care of displaying ProgressBar in a row or
* indicating that it has reached the last row.
*
*/
public abstract class GenericAdapter<T> extends BaseAdapter {
// the main data list to save loaded data
protected List<T> dataList;
protected Activity mActivity;
// the serverListSize is the total number of items on the server side,
// which should be returned from the web request results
protected int serverListSize = -1;
// Two view types which will be used to determine whether a row should be displaying
// data or a Progressbar
public static final int VIEW_TYPE_LOADING = 0;
public static final int VIEW_TYPE_ACTIVITY = 1;
public GenericAdapter(Activity activity, List<T> list) {
mActivity = activity;
dataList = list;
}
public void setServerListSize(int serverListSize){
this.serverListSize = serverListSize;
}
/**
* disable click events on indicating rows
*/
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) == VIEW_TYPE_ACTIVITY;
}
/**
* One type is normal data row, the other type is Progressbar
*/
@Override
public int getViewTypeCount() {
return 2;
}
/**
* the size of the List plus one, the one is the last row, which displays a Progressbar
*/
@Override
public int getCount() {
return dataList.size() + 1;
}
/**
* return the type of the row,
* the last row indicates the user that the ListView is loading more data
*/
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return (position >= dataList.size()) ? VIEW_TYPE_LOADING
: VIEW_TYPE_ACTIVITY;
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? dataList
.get(position) : null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? position
: -1;
}
/**
* returns the correct view
*/
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (getItemViewType(position) == VIEW_TYPE_LOADING) {
// display the last row
return getFooterView(position, convertView, parent);
}
View dataRow = convertView;
dataRow = getDataRow(position, convertView, parent);
return dataRow;
};
/**
* A subclass should override this method to supply the data row.
* @param position
* @param convertView
* @param parent
* @return
*/
public abstract View getDataRow(int position, View convertView, ViewGroup parent);
/**
* returns a View to be displayed in the last row.
* @param position
* @param convertView
* @param parent
* @return
*/
public View getFooterView(int position, View convertView,
ViewGroup parent) {
if (position >= serverListSize && serverListSize > 0) {
// the ListView has reached the last row
TextView tvLastRow = new TextView(mActivity);
tvLastRow.setHint("Reached the last row.");
tvLastRow.setGravity(Gravity.CENTER);
return tvLastRow;
}
View row = convertView;
if (row == null) {
row = mActivity.getLayoutInflater().inflate(
R.layout.progress, parent, false);
}
return row;
}
}
@surfer190

This comment has been minimized.

Copy link

surfer190 commented Jun 5, 2015

What if you don't know the serverListSize?

@hwangjr

This comment has been minimized.

Copy link

hwangjr commented Jun 26, 2015

@surfer190 Just set a trigger~~

@ananth10

This comment has been minimized.

Copy link

ananth10 commented Mar 15, 2016

@hwangjr. can you tell me , how to set trigger?

@ssrishabh96

This comment has been minimized.

Copy link

ssrishabh96 commented Apr 8, 2016

how to set trigger?

@Ni3Narale

This comment has been minimized.

Copy link

Ni3Narale commented Apr 22, 2016

is any one found hot to trigger this class...

@waitray

This comment has been minimized.

Copy link

waitray commented Jun 3, 2016

boolean getAllResult = false;
public void setGetAllResult(boolean getAllResult) {
    this.getAllResult = getAllResult;
}

change "position >= serverListSize && serverListSize > 0" to "if(!getAllResult)" , and u can set "getAllResult" if u get all record from server or db.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.