我正在创建一个自定义ListView,我想在其中显示分隔符的详细信息。我几乎实现了它,但现在每当我到达列表的末尾并且没有正确管理分隔符时它就抛出IndexOutOfBoundException。我试图添加截图以给出一个正确的想法,但我还不是那么受欢迎。
检查一下。你能指出我的错误吗?谢谢!
logcat的:
07-21 21:03:36.597: E/(31016): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
07-21 21:03:42.762: E/AndroidRuntime(31016): FATAL EXCEPTION: main
07-21 21:03:42.762: E/AndroidRuntime(31016): java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
07-21 21:03:42.762: E/AndroidRuntime(31016): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-21 21:03:42.762: E/AndroidRuntime(31016): at java.util.ArrayList.get(ArrayList.java:304)
07-21 21:03:42.762: E/AndroidRuntime(31016): at com.example.contactlist.MainActivity$MyAdapter.getView(MainActivity.java:103)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.widget.AbsListView.obtainView(AbsListView.java:2292)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.widget.ListView.makeAndAddView(ListView.java:2030)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.widget.ListView.fillDown(ListView.java:822)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.widget.ListView.fillGap(ListView.java:786)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5272)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4422)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:766)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.view.Choreographer.doCallbacks(Choreographer.java:575)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.view.Choreographer.doFrame(Choreographer.java:541)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.os.Handler.handleCallback(Handler.java:725)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.os.Handler.dispatchMessage(Handler.java:92)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.os.Looper.loop(Looper.java:158)
07-21 21:03:42.762: E/AndroidRuntime(31016): at android.app.ActivityThread.main(ActivityThread.java:5751)
07-21 21:03:42.762: E/AndroidRuntime(31016): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 21:03:42.762: E/AndroidRuntime(31016): at java.lang.reflect.Method.invoke(Method.java:511)
07-21 21:03:42.762: E/AndroidRuntime(31016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
07-21 21:03:42.762: E/AndroidRuntime(31016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
07-21 21:03:42.762: E/AndroidRuntime(31016): at dalvik.system.NativeStart.main(Native Method)
MainActivity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
in = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
list = (ListView) findViewById(R.id.listView1);
Contacts = new ArrayList<ContactInfo>();
Contacts.add(new ContactInfo("FBC", "1111111111", "#88",
"ABC@.com"));
Contacts.add(new ContactInfo("DEF", "2222222222", "#775",
"DEF@.com"));
Contacts.add(new ContactInfo("EEHI", "3333333333", "#876",
"HHH@.com"));
Contacts.add(new ContactInfo("AAAKL", "5555555555", "#443",
"MAK@.com"));
Contacts.add(new ContactInfo("MNO", "4444444444", "#112",
"HULK@.com"));
Contacts.add(new ContactInfo("BBCC", "666133111", "#828",
"BBCC@.com"));
Contacts.add(new ContactInfo("CCCDEF", "5778322222", "#7175",
"DDCCCEF@.com"));
Contacts.add(new ContactInfo("IIoI", "9877445333", "#8176",
"IIoI@.com"));
Contacts.add(new ContactInfo("QYYKL", "4444442355", "#4143",
"QQYYAK@.com"));
Contacts.add(new ContactInfo("JJKNO", "7786565444", "#1122",
"JJKKL@.com"));
Collections.sort(Contacts, new VideoComparator());
MyAdapter adapter = new MyAdapter();
list.setAdapter(adapter);
}
public static class ViewHolder {
TextView name;
TextView title;
TextView number;
TextView address;
TextView email;
ImageView image;
}
public class MyAdapter extends BaseAdapter {
ViewHolder holder;
public MyAdapter() {
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent) {
if (ConvertView == null) {
holder = new ViewHolder();
ConvertView = in.inflate(R.layout.list_components, parent,false);
holder.name = (TextView) ConvertView.findViewById(R.id.name);
holder.number = (TextView) ConvertView.findViewById(R.id.number);
holder.email = (TextView) ConvertView.findViewById(R.id.email);
holder.address = (TextView) ConvertView.findViewById(R.id.address);
holder.image = (ImageView) ConvertView.findViewById(R.id.Contact_Image);
holder.title = (TextView) ConvertView.findViewById(R.id.Alphabet);
ConvertView.setTag(holder);
} else {
holder = (ViewHolder) ConvertView.getTag();
}
holder.name.setText(Contacts.get(position).getName());
holder.number.setText(Contacts.get(position).getNumber());
holder.email.setText(Contacts.get(position).getEmail());
holder.address.setText(Contacts.get(position).getAddress());
char Name1 = Contacts.get(position).getName().charAt(0);
char Name2 = Contacts.get(position+1).getName().charAt(0);
if(position==0)
{
holder.title.setText("A");
}
else if(!(Name1 == Name2))
{
holder.title.setText(Name2);
}
return ConvertView;
}
@Override
public int getCount() {
return Contacts.size();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
}
答案 0 :(得分:2)
在getView
方法中,您尝试访问Contacts
列表中不存在的元素。
char Name2 = Contacts.get(position+1).getName().charAt(0);
position+1
在呈现列表的最后一个元素时会IndexOutOfBoundsException
。
答案 1 :(得分:0)
char Name2 = Contacts.get(position+1).getName().charAt(0);
您正在寻找下一个Contact
,当您到达列表末尾时,这将产生ArrayIndexOutOfBoundsException
。
答案 2 :(得分:0)
错误就是这一行:
char Name2 = Contacts.get(position+1).getName().charAt(0);
使用
验证此行if (position < Contacts.size()) {
char Name2 = Contacts.get(position+1).getName().charAt(0);
}
答案 3 :(得分:0)
我终于解决了这个问题。
if(position>0 && position < Contacts.size()){
char c1 = Contacts.get(position-1).getName().charAt(0);
char c2 = Contacts.get(position).getName().charAt(0);
if(c1 == c2){
holder.title.setText("");
}else{
holder.title.setText(String.valueOf(c2));
}
}