我得到arrayIndexOutOfBound异常,如果数组的大小在适配器中为零,在行holder.checkbox.setChecked(bmpArray.get(position).isChecked());
中给出错误
package com.algosoft.zobazar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.algosoft.library.GridItem;
import com.algosoft.library.ImageData;
import com.algosoft.library.ImageLoader;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by ADMIN on 4/9/2016.
*/
public class GridSellerAdapter extends ArrayAdapter<String> {
private Context context;
private ArrayList<ImageData> bmpArray;
private ImageLoader2 imageLoader;
private int width;
private LayoutInflater inflater;
protected int visibleFlag;
public boolean longFlag;
public HashMap<String, String> hmp;
private LinearLayout btnDeleteLayout;
private boolean loadAll = false;
public GridSellerAdapter(Activity camera, ArrayList<ImageData> selectedSongs, LinearLayout btnDeleteLayout) {
super(camera, R.layout.activity_seller__upload_product);
this.context = camera;
this.bmpArray = selectedSongs;
this.btnDeleteLayout = btnDeleteLayout;
width = ScreenDensity.Width(context);
imageLoader = new ImageLoader2(camera.getApplicationContext(),width/3,width/3);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
visibleFlag = View.GONE;
longFlag = false;
hmp = new HashMap<String, String>();
btnDeleteLayout.setVisibility(View.GONE);
}
public void setTrue(){
loadAll = true;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(loadAll) return bmpArray.size();
return 3;
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(
R.layout.multiple_files_choice, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setTag(position);
holder.imageview.setTag(position);
holder.checkbox.setTag(position);
holder.imageview.setOnClickListener(null);
holder.imageview.setOnLongClickListener(null);
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setChecked(bmpArray.get(position).isChecked());
imageLoader.DisplayImage(bmpArray.get(position).getPath(), holder.imageview);
holder.id = position;
holder.imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(longFlag){
if(!holder.checkbox.isChecked()){
holder.checkbox.setChecked(true);
hmp.put(bmpArray.get(position).getPath(), bmpArray.get(position).getPath());
if(hmp.size()>0)
btnDeleteLayout.setVisibility(View.VISIBLE);
}else{
holder.checkbox.setChecked(false);
hmp.remove(bmpArray.get(position));
if(hmp.size()<1)
btnDeleteLayout.setVisibility(View.GONE);
}
}
else{
Intent i = new Intent(context, MyGallery.class);
i.putExtra("position", position);
context.startActivity(i);
((Activity) context).overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
}
}
});
holder.imageview.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
if(!longFlag){
hmp.clear();
holder.checkbox.setChecked(true);
hmp.put(bmpArray.get(position).getPath(), bmpArray.get(position).getPath());
longFlag = true;
visibleFlag = View.VISIBLE;
notifyDataSetChanged();
}
return false;
}
});
holder.checkbox.setVisibility(visibleFlag);
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition = (Integer) buttonView.getTag();
bmpArray.get(getPosition).setChecked(buttonView.isChecked());
if(isChecked){
hmp.put(bmpArray.get(position).getPath(), bmpArray.get(position).getPath());
if(hmp.size()>0)
btnDeleteLayout.setVisibility(View.VISIBLE);
}else{
hmp.remove(bmpArray.get(position).getPath());
if(hmp.size()<1)
btnDeleteLayout.setVisibility(View.GONE);
}
}
});
return convertView;
}
}
答案 0 :(得分:0)
问题出在这里
@Override
public int getCount() {
// TODO Auto-generated method stub
if(loadAll) return bmpArray.size();
return 3;
}
应该 return 0
而不是 return 3
@Override
public int getCount() {
// TODO Auto-generated method stub
if(loadAll) return bmpArray.size();
return 0;
}
如果您在bmpArray
中有明确的价值,请直接使用return bmpArray.size()
@Override
public int getCount() {
return null != bmpArray ? bmpArray.size() : 0;
}
或者,如果您从loadAll
或Fragment
设置Activity
,那么您应该使用GridSellerAdapter.notifyDataSetChanged();
而不是更新getCount()
中的值。但是,bmpArray
的值已从其加载其值的Activity
或Fragment
更改后。
答案 1 :(得分:0)
在getCount()方法中你写了这样的条件。
@Override
public int getCount() {
// TODO Auto-generated method stub
if(loadAll) return bmpArray.size();
return 3;
}
如果loadall为true / false,则bmpArray不应为null。根据loadall值,您尝试加载值。如果loadall值设置为false,那么你试图加载有限的值。总是你应该在bmpArrray中有值。
检查这样的情况..
@Override
public int getCount() {
// TODO Auto-generated method stub
if(loadAll){
return bmpArray.size();
}else{
if(bmpArray.size()<3){
return bmpArray.size();
}else{
return 3;
}
}
}