这是我的代码,我得到的数组超出了绑定异常。我解决了这个错误?我使用了Bitmap对象而不是字符串。当我将位图图像存储到数组列表中时,我收到了这个错误。
MainActivity.java
public class MainActivity extends Activity {
ListView lv;
Context context;
ArrayList prgmName;
Bitmap bitmap;
//bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");
context=this;
Bitmap [] prgmImages ={bitmap,bitmap};
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Bitmap getBitmapFromUrl(String src) {
// TODO Auto-generated method stub
try {
URL url =new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input=connection.getInputStream();
Bitmap mybiBitmap=BitmapFactory.decodeStream(input);
return mybiBitmap;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}
CustomerAdapter.java
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
Bitmap[] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, Bitmap[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageBitmap(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
答案 0 :(得分:2)
您获得ArrayIndexOutOfBound,因为 imageId 的数据大小不是结果。
你可以看到你的结果数组有9个元素而 imageId 只有2个元素,并且两个元素都相同,那你为什么要使用数组,只需传递简单的位图代替数组。
你的结果数组:
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
你的imageId数组:
Bitmap [] prgmImages ={bitmap,bitmap};
解决方案:
1)如果要为行项目显示不同的图像,则传递 imageId 数组相同的大小 结果数组。 2)如果要在所有行项目中显示相同的图像,则只传递单个位图而不是数组。
答案 1 :(得分:0)
您的字符串数组有9个项目。所以你的适配器在getCount()
中返回9public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
但是你的位图数组只有2个项目
Bitmap [] prgmImages ={bitmap,bitmap};
因此,在getView
方法中,当listView请求创建列表中的第三个项目时,您将收到此行的界限。
holder.img.setImageBitmap(imageId[position]);
要修复它,您必须扩展图像阵列。它的大小必须与字符串数组的大小匹配。
答案 2 :(得分:0)
试试这个......
if(result != null && result.length > position)
{
holder.tv.setText(result[position]);
}
if(imageId != null && imageId.length > 0)
{
holder.img.setImageBitmap(imageId[0]);
}
这一切都取决于你从getCount()返回的数量 如果你返回10并且你访问数组的第10个元素(例如arr [position])但是提到的数组大小是2则它会抛出异常。