Repository navigation
Support generics in @InstanceState #113
Description
Activity
Is it the same thing with supporting generics for @EActivity in general?
I cant use Android Annotations on a Generic Activity at all.Hello @funk78. I think we just never thought of generic activities, because they are created by the Android container. Is that an abstract activity, or a normal activity ? What do you use that for ?
Right now its an abstract class, because i needed a workaround for android annotations to use @UiThread and @background. But it should be a normal activity.
I use it like this:
public abstract class EndlessListActivity<T> extends ListActivity { public interface EndlessListActivityContentProvider<T> { List<T> getNextItems(Integer offset); } private final List<T> listData = new ArrayList<T>(); private boolean isLoadingMoreDataFromService; private boolean stopLoadingRecords = false; private ArrayAdapter<T> listItemAdapter; private int lastOffset = -123; private EndlessListActivityContentProvider<T> contentProvider; private MyToast toast; public void afterViews(EndlessListActivityContentProvider<T> content, MyToast toast) { this.contentProvider = content; this.toast = toast; this.listItemAdapter = initListAdapter(); setListAdapter(this.listItemAdapter); callLoadListItemsTaskInBackground(0); toast.showToast(R.string.toast_initial_download_triggered); initOnScrollReloadListener(); } public void attachToListView(List<T> records) { this.listData.addAll(records); EndlessListActivity.this.listItemAdapter.notifyDataSetChanged(); this.isLoadingMoreDataFromService = false; if (records.size() < Cfg.listChunkSize || this.listData.size() >= Cfg.ListViewMaxSize) { this.stopLoadingRecords = true; } else { callLoadMoreIfSpaceAvailableInUiThread(); } } public abstract void callAttachToListViewInUiThread(List<T> items); public abstract void callLoadListItemsTaskInBackground(int offset); public abstract void callLoadMoreIfSpaceAvailableInUiThread(); public T getItemAtPosition(int position) { return this.listData.get(position); } public List<T> getListData() { return this.listData; } public View getListItem(View convertView, int layoutId) { View listItem; if (convertView == null) { LayoutInflater inflater = getLayoutInflater(); listItem = inflater.inflate(layoutId, null, true); } else { listItem = convertView; } return listItem; } public abstract ArrayAdapter<T> initListAdapter(); public void initOnScrollReloadListener() { getListView().setOnScrollListener(new OnScrollListener() { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { EndlessListActivity<T> activity = EndlessListActivity.this; if (activity.isLoadingMoreDataFromService || activity.stopLoadingRecords) { return; } if (visibleItemCount == totalItemCount) { loadMore(totalItemCount); return; } if (totalItemCount - Cfg.listChunkSize < firstVisibleItem + visibleItemCount) { loadMore(totalItemCount); return; } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) {} private void loadMore(int totalItemCount) { EndlessListActivity.this.isLoadingMoreDataFromService = true; callLoadListItemsTaskInBackground(totalItemCount); } }); } public void loadListItemsTask(Integer offset) { if (this.lastOffset == offset) { return; } this.lastOffset = offset; List<T> items = null; items = this.contentProvider.getNextItems(offset); if (items == null) { this.toast.showToast(R.string.toast_download_records_error); } callAttachToListViewInUiThread(items); } public void loadMoreIfSpaceAvailable() { int count = getListView().getAdapter().getCount(); int lastVisiblePos = getListView().getLastVisiblePosition(); if (lastVisiblePos == count) { callLoadListItemsTaskInBackground(count); } } }
Ok, thanks @funk78, could you create a separate issue for that :) ?
@matboniface, regarding generics and
@InstanceState, is that something you could fix ?I already started to work on that issue.
It's not so easy to handle it, I hope I can provide a fix next week.
- added a commit that references this issue
on Mar 17, 2012
Generics fields don't work yet with
@InstanceState:It generates the following compile error :
Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable.We decided to merge pull request #101 anyway (issue #77), and we'll handle generics in a new branch.