嗨,我正在制作一个应用程序,我必须在其中使用带有 gridview 的滚动。我想将滚动限制设置为六个图像,即当我滚动时,我应该在 gridview 中获得六个图像。但我在 Scroll 上得到 Array Index Out of Bound。我的代码如下:
public class ListMobileActivity extends Activity {
public static int items=14;
private boolean loadingMore=false;
public static ArrayList<String> url;
public static ArrayList<String> name;
NewSaxParserActivity activity;
String[] values;
MobileArrayAdapter adapter;
GridView gridview;
int x=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("welcome to gridview");
setContentView(R.layout.categorygridview);
savedInstanceState=this.getIntent().getExtras();
String string=String.valueOf(savedInstanceState.getInt("id"));
String string1=getIntent().getStringExtra("name");
String string2=getIntent().getStringExtra("name1");
TextView textView=(TextView)findViewById(R.id.catName);
textView.setText(string2);
gridview=(GridView)findViewById(R.id.gridfour);
activity=new NewSaxParserActivity();
activity.display(string1,string);
url=new ArrayList<String>();
name=new ArrayList<String>();
name=activity.name;*/
adapter=new MobileArrayAdapter(ListMobileActivity.this,url,name);
gridview.setAdapter(adapter);
gridview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
System.out.println("lastinscreen::::"+lastInScreen);
if((lastInScreen == totalItemCount) && !(loadingMore)){
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
});
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
private Runnable loadMoreListItems = new Runnable() {
@Override
public void run() {
int x=0;
loadingMore = true;
url = new ArrayList<String>();
name = new ArrayList<String>();
System.out.println("url1 size :"+activity.url1.size());
try { Thread.sleep(1000);
} catch (InterruptedException e) {}
for (int i = 0; i < items
; i++) {
System.out.println("x :"+x);
url.add(activity.url1.get(x).toString());
name.add(activity.name.get(x).toString());
x=x+1;
}
runOnUiThread(returnRes);
}
};
private Runnable returnRes = new Runnable() {
@Override
public void run() {
try{
if(url != null && url.size() > 0){
System.out.println("size :"+url.size());
System.out.println("size :"+name.size());
for(int i=0;i<url.size();i++)
{System.out.println("url :"+url.get(i));
System.out.println("name :"+name.get(i));
adapter.add(url.get(i));
adapter.add(name.get(i));
}
}
System.out.println("size :"+url.size());
System.out.println("size :"+name.size());
}
catch (Exception e) {
// TODO: handle exception
e.getMessage();
}
adapter.notifyDataSetChanged();
loadingMore = false;
}
};
}
上面是我的主要活动,下面是我的适配器
public class MobileArrayAdapter extends ArrayAdapter<String> {
Context context;
String[] values1;
String[] name1;
Activity activity;
ImageLoader imageLoader;
public MobileArrayAdapter(Context context, ArrayList<String> values, ArrayList<String> name) {
super(context, R.layout.main, values);
this.context=context;
values1=new String[values.size()];
name1=new String[name.size()];
Object []arr={"hello"};
Object []arr1={"hello"};
arr=values.toArray(arr);
arr1=name.toArray(arr1);
activity=(Activity)this.getContext();;
for(int i=0;i<values.size();i++)
{
values1[i]=arr[i].toString();
name1[i]=arr1[i].toString();
}
this.activity=(Activity)this.getContext();
imageLoader=new ImageLoader(activity.getApplicationContext());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
System.out.println("getView");
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.list_mobile, parent, false);
holder=new ViewHolder();
holder.image=(ImageView)vi.findViewById(R.id.logo);
holder.text=(TextView)vi.findViewById(R.id.label);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
System.out.println("position :"+position);
System.out.println("name :"+name1.length);
System.out.println("values :"+values1.length);
System.out.println("name on position :"+name1[position]);
holder.text.setText(name1[position]);
holder.image.setTag(values1[position]);
imageLoader.DisplayImage(values1[position], activity, holder.image);
return vi;
}
public static class ViewHolder{
public TextView text;
public ImageView image;
}
*@Override
public String getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getPosition(String item) {
return super.getPosition(item);
}
}
以下是例外:
06-11 10:15:22.013: E/AndroidRuntime(680): FATAL EXCEPTION: main
06-11 10:15:22.013: E/AndroidRuntime(680): java.lang.ArrayIndexOutOfBoundsException
06-11 10:15:22.013: E/AndroidRuntime(680): at com.trigma.pp.MobileArrayAdapter.getView(MobileArrayAdapter.java:77)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.GridView.onMeasure(GridView.java:932)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.View.measure(View.java:8171)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.View.measure(View.java:8171)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.View.measure(View.java:8171)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.View.measure(View.java:8171)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.View.measure(View.java:8171)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.os.Looper.loop(Looper.java:123)
06-11 10:15:22.013: E/AndroidRuntime(680): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-11 10:15:22.013: E/AndroidRuntime(680): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 10:15:22.013: E/AndroidRuntime(680): at java.lang.reflect.Method.invoke(Method.java:521)
06-11 10:15:22.013: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-11 10:15:22.013: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-11 10:15:22.013: E/AndroidRuntime(680): at dalvik.system.NativeStart.main(Native Method)