MainActivity.java
public class MainActivity extends ListActivity {
private LinkedList<LinkedHashMap<String, String>> messengerListObject;
private LinkedHashMap<String, String> messengerMapObject;
static int LIST_SIZE;
private int mLastOffset = 0;
static final int BATCH_SIZE = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addDataForCollection();
init();
}
private void setLastOffset(int i) {
mLastOffset = i;
}
private int getLastOffset(){
return mLastOffset;
}
private void init() {
//Store the array size in a variable
LIST_SIZE = messengerListObject.size();
//Load the first 10 elements from the array
for (int i=0; i<=BATCH_SIZE; i++){
messengerMapObject=messengerListObject.get(i);
}
setLastOffset(BATCH_SIZE);
displayList(messengerMapObject);
}
private void displayList(LinkedHashMap<String, String> messengerMapObject) {
setListAdapter(new DemoAdapter());
}
class DemoAdapter extends EndlessAdapter {
private RotateAnimation rotate=null;
LinkedList<LinkedHashMap<String, String>> tempList=new LinkedList<LinkedHashMap<String, String>>();
DemoAdapter() {
super(new AdapterMain(MainActivity.this, messengerMapObject));
rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
@Override
protected View getPendingView(ViewGroup parent) {
View row=getLayoutInflater().inflate(R.layout.row, null);
View child=row.findViewById(android.R.id.text1);
child.setVisibility(View.GONE);
child=row.findViewById(R.id.throbber);
child.setVisibility(View.VISIBLE);
child.startAnimation(rotate);
return(row);
}
@Override
protected boolean cacheInBackground() {
tempList.clear();
int lastOffset = getLastOffset();
if(lastOffset < LIST_SIZE){
int limit = lastOffset + BATCH_SIZE;
for(int i=(lastOffset+1); (i<=limit && i<LIST_SIZE); i++){
tempList.add(messengerListObject.get(i));
}
setLastOffset(limit);
if(limit<LIST_SIZE){
return true;
} else {
return false;
}
} else {
return false;
}
}
@Override
protected void appendCachedData() {
@SuppressWarnings("unchecked")
LinkedList<LinkedHashMap<String, String>> arrAdapterNew = (LinkedList<LinkedHashMap<String, String>>)getWrappedAdapter();
int listLen = tempList.size();
for(int i=0; i<listLen; i++){
arrAdapterNew.add(tempList.get(i));
}
}
}
private void addDataForCollection() {
messengerListObject=new LinkedList<LinkedHashMap<String, String>>();
for(int i=1;i<100;i++){
messengerMapObject=new LinkedHashMap<String, String>();
messengerMapObject.put("android", "Android-"+i+"");
messengerListObject.add(messengerMapObject);
}
}
}
AdapterMain.java
public class AdapterMain extends BaseAdapter {
private Context mContext=null;
private HashMap<String, String> messengerMapObject;
public AdapterMain(Context context, LinkedHashMap<String, String> _messengerMapObject) {
super();
mContext = context;
messengerMapObject=_messengerMapObject;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
public TextView txtTitle;
public ViewHolder(View base) {
txtTitle = (TextView) base.findViewById(R.id.txtTitle);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder vHolder;
if (convertView == null) {
LayoutInflater layout = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layout.inflate(R.layout.adapter, null);
vHolder = new ViewHolder(view);
view.setTag(vHolder);
} else {
vHolder = (ViewHolder) view.getTag();
}
vHolder.txtTitle.setText(messengerMapObject.get("android"));
return view;
}
}
日志 ::
08-05 11:31:30.976: E/AndroidRuntime(19968): FATAL EXCEPTION: main
08-05 11:31:30.976: E/AndroidRuntime(19968): java.lang.ClassCastException: com.example.endlessadapter.AdapterMain cannot be cast to java.util.LinkedList
08-05 11:31:30.976: E/AndroidRuntime(19968): at com.example.endlessadapter.MainActivity$DemoAdapter.appendCachedData(MainActivity.java:113)
08-05 11:31:30.976: E/AndroidRuntime(19968): at com.commonsware.cwac.endless.EndlessAdapter$AppendTask.onPostExecute(EndlessAdapter.java:343)
08-05 11:31:30.976: E/AndroidRuntime(19968): at com.commonsware.cwac.endless.EndlessAdapter$AppendTask.onPostExecute(EndlessAdapter.java:1)
08-05 11:31:30.976: E/AndroidRuntime(19968): at android.os.AsyncTask.finish(AsyncTask.java:602)
08-05 11:31:30.976: E/AndroidRuntime(19968): at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-05 11:31:30.976: E/AndroidRuntime(19968): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-05 11:31:30.976: E/AndroidRuntime(19968): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:31:30.976: E/AndroidRuntime(19968): at android.os.Looper.loop(Looper.java:137)
08-05 11:31:30.976: E/AndroidRuntime(19968): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-05 11:31:30.976: E/AndroidRuntime(19968): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:31:30.976: E/AndroidRuntime(19968): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:31:30.976: E/AndroidRuntime(19968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-05 11:31:30.976: E/AndroidRuntime(19968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-05 11:31:30.976: E/AndroidRuntime(19968): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我认为你的问题在这里:
这是Commonsware无限的Adapter构造函数的定义:
public EndlessAdapter(Context ctx, List<String> itemList, int layoutId)
但你发送给它
super(new AdapterMain(MainActivity.this, messengerMapObject));
哪个
private LinkedHashMap<String, String> messengerMapObject;
答案 1 :(得分:0)
您还可以查看SuperListView库的代码。
SuperListView支持无限滚动。 见https://github.com/dommerq/SuperListview