gpt4 book ai didi

java - Android 自定义 ListView 适配器中的 ArrayList 索引越界异常

转载 作者:行者123 更新时间:2023-12-01 12:43:26 25 4
gpt4 key购买 nike

这是我的下面的代码,我的列表如下

position 1  |  image1      image2      image3
----------------------------------------
position 2 | image1 image2 image3

我的意思是根据列表中的项目数量动态添加图像,例如,如果我的列表有 21 个项目,则 mylist 在每个位置填充 3 个图像,总共创建 7 个位置。我的代码运行良好

 public class ListViewAdapter extends BaseAdapter {
private static final String TAG = "Book-shelf";
private Activity activity;
private int totalBooks = 0;
private int bookItem = 0;
private int numberOfBookRows = 0;
private int extraBooks = 0;
int layoutId;
private int bookImage;
private ArrayList<HashMap<String, String>> dataList;
private static LayoutInflater inflater = null;

public ListViewAdapter(Activity a, int listRow,
ArrayList<HashMap<String, String>> fileList) {
super();

activity = a;
dataList = fileList;
layoutId = listRow;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}

修改的是上面的getCount方法

   @Override
public Object getCount() {
// TODO Auto-generated method stub
return numberOfBookRows;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public void calculateRowsAndColms() {
totalBooks = 0;
bookItem = 0;
numberOfBookRows = 0;
extraBooks = 0;

totalBooks = dataList.size();
bookItem = 3;// TODO: make it dynamic
if (totalBooks < bookItem && totalBooks > 0) {
numberOfBookRows = 1;
} else {
numberOfBookRows = (totalBooks / bookItem);
extraBooks = (totalBooks % bookItem);
if (extraBooks != 0)
numberOfBookRows++;
}

Log.d(TAG, " Total books:" + totalBooks);
Log.d(TAG, " Number of books to be displayed:" + bookItem);
Log.d(TAG, " Number of rows occupying the books:" + numberOfBookRows);
Log.d(TAG, " Number of extra books(odd count):" + extraBooks);
}

这是我的arrayadapter的查看代码

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;
View v = convertView;
Log.d("View", "view" + v);
calculateRowsAndColms();
bookImage = R.drawable.book1;
Log.d("Datalist","size="+dataList.size());
if (convertView == null) {
holder = new ViewHolder();
v = inflater.inflate(layoutId, null, false);

ImageView image1 = (ImageView) v.findViewById(R.id.image1);
ImageView image2 = (ImageView) v.findViewById(R.id.image2);
ImageView image3 = (ImageView) v.findViewById(R.id.image3);

TextView t1 = (TextView) v.findViewById(R.id.textView1);
TextView t2 = (TextView) v.findViewById(R.id.textView2);
TextView t3 = (TextView) v.findViewById(R.id.textView3);

holder.imgList.add(image1);
holder.imgList.add(image2);
holder.imgList.add(image3);

holder.textList.add(t1);
holder.textList.add(t2);
holder.textList.add(t3);

v.setTag(holder);

} else {
holder = (ViewHolder) v.getTag();
}
int k;

if ((position == numberOfBookRows - 1) && extraBooks != 0) {


for (int i = 0; i < extraBooks; i++) {
Log.d("inside if", "position" + position);
Log.d("inside if", "numberofbookrows" + numberOfBookRows);
int k = (position * bookItem) + i;
holder.imgList.get(i).setBackgroundResource(bookImage);
holder.imgList.get(i).setOnClickListener(
new OnBookClickListner());
holder.imgList.get(i).setVisibility(View.VISIBLE);
holder.imgList.get(i).setId(k);
holder.textList.get(i).setText(dataList.get(k).get("pdf"));
Log.d("if k value","k="+k+"value="+dataList.get(k).get("pdf"));

}

} else if((position <= numberOfBookRows - 1) && totalBooks<bookItem)
{

for (int i = 0; i < totalBooks; i++) {

Log.d("inside else", "position" + position);
Log.d("inside else", "numberofbookrows" + numberOfBookRows);
int k = (position * bookItem) + i;
holder.imgList.get(i).setBackgroundResource(bookImage);
holder.imgList.get(i).setOnClickListener(
new OnBookClickListner());
holder.imgList.get(i).setVisibility(View.VISIBLE);
holder.imgList.get(i).setId(k);
holder.textList.get(i).setText(dataList.get(k).get("pdf"));
Log.d("2nd if k value","k="+k+"value="+dataList.get(k).get("pdf"));

}
}
else
{
for (int i = 0; i < bookItem; i++) {
int k = (position * bookItem) + i;
holder.imgList.get(i).setBackgroundResource(bookImage);
holder.imgList.get(i).setOnClickListener(
new OnBookClickListner());
holder.imgList.get(i).setVisibility(View.VISIBLE);
holder.imgList.get(i).setId(k);
holder.textList.get(i).setText(dataList.get(k).get("pdf"));
Log.d("3rd if k value","k="+k+"value="+dataList.get(k).get("pdf"));

}

}

return v;

}

static class ViewHolder {
ArrayList<ImageView> imgList = new ArrayList<ImageView>();
ArrayList<TextView> textList = new ArrayList<TextView>();

// ImageView image1, image2 ,image3;
}

public class OnBookClickListner implements OnClickListener {

@Override
public void onClick(View v) {
Log.d(TAG, " ITEM SELECTED IN LIST VIEW:" + v.getId());
/*
* Intent intent = new Intent(MainActivity.this,
* MainActivity.class); startActivity(intent);
*/
}

}

}

现在,当我运行此代码时,我得到了预期的输出,但是当我尝试滚动列表时,应用程序关闭,我收到致命异常

    07-22 03:57:26.501: E/AndroidRuntime(1314): FATAL EXCEPTION: main
07-22 03:57:26.501: E/AndroidRuntime(1314): java.lang.IndexOutOfBoundsException: Invalid index 21, size is 20
07-22 03:57:26.501: E/AndroidRuntime(1314): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
07-22 03:57:26.501: E/AndroidRuntime(1314): at java.util.ArrayList.get(ArrayList.java:308)
07-22 03:57:26.501: E/AndroidRuntime(1314): at com.example.samplebookshelfdesign.ListViewAdapter.getView(ListViewAdapter.java:151)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.widget.AbsListView.obtainView(AbsListView.java:2161)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.widget.ListView.makeAndAddView(ListView.java:1840)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.widget.ListView.fillDown(ListView.java:675)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.widget.ListView.fillGap(ListView.java:639)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4970)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3126)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3400)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.View.dispatchTouchEvent(View.java:7384)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2203)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1938)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
07-22 03:57:26.501: E/AndroidRuntime(1314): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
07-22 03:57:26.501: E/AndroidRuntime(1314): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
07-22 03:57:26.501: E/AndroidRuntime(1314): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.View.dispatchPointerEvent(View.java:7564)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
07-22 03:57:26.501: E/AndroidRuntime(1314): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)

最佳答案

问题出在你的 if/else 条件上。您不处理 position 超过行数的情况。

if ((position == numberOfBookRows - 1) && extraBooks != 0) {
// ...
} else {
// This will also get executed if position > numberOfBooksRows -1
}

您可能应该添加一个额外的防护:

if ((position == numberOfBookRows - 1) && extraBooks != 0) {
// ...
} else if (position < numberOfBookRows - 1) {
// ...
}

关于java - Android 自定义 ListView 适配器中的 ArrayList 索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24882250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com