我正在尝试使用无限的适配器,我按照这个例子
http://droidista.blogspot.com/2011/04/using-cwacs-endlessadapter-with-custom.html
和
我试过这段代码
public class MainActivity extends Activity {
static int LIST_SIZE;
private int mLastOffset = 0;
static final int BATCH_SIZE = 10;
private RotateAnimation rotate = null;
ArrayList<String> tempList = new ArrayList<String>();
String[] product = new String[]{"P1" , "P2" , "P3" , "P4" , "P5" ,"P6" ,
"P7" , "P8" , "P9" , "P10" , "P11" ,"P12" ,
"P13" , "P14" , "P15" , "P16" , "P17" ,"P18" ,
"P19" , "P20" , "P21" , "P22" , "P23" ,"P24" ,
"P1" , "P2" , "P3" , "P4" , "P5" ,"P6" ,
"P7" , "P8" , "P9" , "P10" , "P11" ,"P12" ,
"P13" , "P14" , "P15" , "P16" , "P17" ,"P18" ,
"P19" , "P20" , "P21" , "P22" , "P23" ,"P24" ,
"Val1" , "Val2" , "Val3" , "Val4" , "Val5" ,"Val6" ,
"Val7" , "Val8" , "Val9" , "Val10" , "Val11" ,"Val12" ,
"Val13" , "Val14" , "Val15" , "Val16" , "Val17" ,"Val18" ,
"Val19" , "Val20" , "Val21" , "Val22" , "Val23" ,"Val24" ,
"Val1" , "Val2" , "Val3" , "Val4" , "Val5" ,"Val6" ,
"Val7" , "Val8" , "Val9" , "Val10" , "Val11" ,"Val12" ,
"Val13" , "Val14" , "Val15" , "Val16" , "Val17" ,"Val18" ,
"Val19" , "Val20" , "Val21" , "Val22" , "Val23" ,"Val24"};
String[] shop_items = new String[]{"Val1" , "Val2" , "Val3" , "Val4" , "Val5" ,"Val6" ,
"Val7" , "Val8" , "Val9" , "Val10" , "Val11" ,"Val12" ,
"Val13" , "Val14" , "Val15" , "Val16" , "Val17" ,"Val18" ,
"Val19" , "Val20" , "Val21" , "Val22" , "Val23" ,"Val24" ,
"Val1" , "Val2" , "Val3" , "Val4" , "Val5" ,"Val6" ,
"Val7" , "Val8" , "Val9" , "Val10" , "Val11" ,"Val12" ,
"Val13" , "Val14" , "Val15" , "Val16" , "Val17" ,"Val18" ,
"Val19" , "Val20" , "Val21" , "Val22" , "Val23" ,"Val24" ,
"P1" , "P2" , "P3" , "P4" , "P5" ,"P6" ,
"P7" , "P8" , "P9" , "P10" , "P11" ,"P12" ,
"P13" , "P14" , "P15" , "P16" , "P17" ,"P18" ,
"P19" , "P20" , "P21" , "P22" , "P23" ,"P24" ,
"P1" , "P2" , "P3" , "P4" , "P5" ,"P6" ,
"P7" , "P8" , "P9" , "P10" , "P11" ,"P12" ,
"P13" , "P14" , "P15" , "P16" , "P17" ,"P18" ,
"P19" , "P20" , "P21" , "P22" , "P23" ,"P24"};
ArrayList<String> ProdcutList1 = new ArrayList<String>();
ArrayList<String> ValList1 = new ArrayList<String>();
ListView SimpleList ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleList = (ListView)findViewById(R.id.listView1);
init();
//DisplayList();
for (int i=0; i<product.length; i++){
ValList1.add(product[i]);
}
}
private void init() {
LIST_SIZE = shop_items.length;
for (int i=0; i<=BATCH_SIZE; i++){
ProdcutList1.add(shop_items[i]);
// ValList1.add(product[i]);
}
setLastOffset(BATCH_SIZE);
displayList(ProdcutList1);
}
private void setLastOffset(int i) {
mLastOffset = i;
}
private int getLastOffset(){
return mLastOffset;
}
private void displayList(ArrayList<String> ProdcutList1) {
SimpleList.setAdapter(new DemoAdapter());
}
private class CustomArrayAdapter extends ArrayAdapter<String>{
public CustomArrayAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater)
MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.set_aside_items, null);
holder = new ViewHolder();
holder.word = (TextView)convertView.findViewById(R.id.producttitle);
holder.world = (TextView)convertView.findViewById(R.id.price);
convertView.setTag(holder);
} else{
holder=(ViewHolder)convertView.getTag();
}
holder.word.setText(String.valueOf(position+1) + ". " + ProdcutList1.get(position));
holder.world.setText(String.valueOf(position+1) + ". " + ValList1.get(position));
return convertView;
}
public class ViewHolder{
public TextView word , world;
}
}
class DemoAdapter extends EndlessAdapter {
private RotateAnimation rotate=null;
private View pendingView=null;
ArrayList<String> tempList1 = new ArrayList<String>();
ArrayList<String> tempList2 = new ArrayList<String>();
DemoAdapter() {
super(new CustomArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, ProdcutList1));
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() {
tempList1.clear();
tempList2.clear();
int lastOffset = getLastOffset();
if(lastOffset < LIST_SIZE){
int limit = lastOffset + BATCH_SIZE;
for(int i=(lastOffset+1); (i<=limit && i<LIST_SIZE); i++){
tempList1.add(shop_items[i]);
tempList2.add(product[i]);
}
setLastOffset(limit);
if(limit<LIST_SIZE){
return true;
} else {
return false;
}
} else {
return false;
}
}
@Override
protected void appendCachedData() {
@SuppressWarnings("unchecked")
ArrayAdapter<String> arrAdapterNew1 = (ArrayAdapter<String>)getWrappedAdapter();
@SuppressWarnings("unchecked")
ArrayAdapter<String> arrAdapterNew2 = (ArrayAdapter<String>)getWrappedAdapter();
int listLen = tempList1.size();
for(int i=0; i<listLen; i++){
arrAdapterNew1.add(tempList1.get(i));
// arrAdapterNew1.
arrAdapterNew1.add(tempList2.get(i));
arrAdapterNew1.notifyDataSetChanged();
arrAdapterNew2.add(tempList2.get(i));
displayList(tempList1);
}
// }
}
void startProgressAnimation() {
if (pendingView!=null) {
pendingView.startAnimation(rotate);
}
}
}
}
但是这个代码仅适用于滚动后的前十个值,它会产生以下错误
05-02 15:24:54.031: E/AndroidRuntime(3001): FATAL EXCEPTION: main
05-02 15:24:54.031: E/AndroidRuntime(3001): java.lang.IndexOutOfBoundsException: Invalid index 11, size is 11
05-02 15:24:54.031: E/AndroidRuntime(3001): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
05-02 15:24:54.031: E/AndroidRuntime(3001): at java.util.ArrayList.get(ArrayList.java:304)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.example.smaple_endlessadapter.MainActivity$CustomArrayAdapter.getView(MainActivity.java:145)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.commonsware.cwac.adapter.AdapterWrapper.getView(AdapterWrapper.java:117)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.commonsware.cwac.endless.EndlessAdapter.getView(EndlessAdapter.java:261)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.ListView.makeAndAddView(ListView.java:1831)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.ListView.fillDown(ListView.java:674)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.ListView.fillGap(ListView.java:638)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.AbsListView.startScrollIfNeeded(AbsListView.java:3032)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3357)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.View.dispatchTouchEvent(View.java:7246)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.View.dispatchPointerEvent(View.java:7426)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.Choreographer.doFrame(Choreographer.java:530)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.os.Handler.handleCallback(Handler.java:725)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.os.Looper.loop(Looper.java:137)
05-02 15:24:54.031: E/AndroidRuntime(3001): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-02 15:24:54.031: E/AndroidRuntime(3001): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 15:24:54.031: E/AndroidRuntime(3001): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-02 15:24:54.031: E/AndroidRuntime(3001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-02 15:24:54.031: E/AndroidRuntime(3001): at dalvik.system.NativeStart.main(Native Method)
如何使用无限适配器创建自定义列表?