0

我在点击 listView 项目时遇到异常..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    adapter = new MyAdapter(this,R.layout.list_item);
    listView = (ListView)findViewById(R.id.listView);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adaptr, View arg1, int position, long arg3) {

    Times times = (Times)adaptr.getItemAtPosition(position);
    //Times times = (Times)MyXMLHandler.array.get(position);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(times.getDescription());
    builder.setTitle("Description");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            finish();
        }
    });
}

错误.......

06-20 14:55:13.137: ERROR/AndroidRuntime(848): FATAL EXCEPTION: main
06-20 14:55:13.137: ERROR/AndroidRuntime(848): java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0
06-20 14:55:13.137: ERROR/AndroidRuntime(848):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
06-20 14:55:13.137: ERROR/AndroidRuntime(848):     at java.util.ArrayList.get(ArrayList.java:311)
06-20 14:55:13.137: ERROR/AndroidRuntime(848):     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
06-20 14:55:13.137: ERROR/AndroidRuntime(848):     at android.widget.AdapterView.getItemAtPosition(AdapterView.java:740)
4

2 回答 2

0

您忘记在适配器中填写数据您的适配器为空..

adapter = new MyAdapter(this,R.layout.list_item);

填写一些数据,例如String[]ArrayList...

更新:

@Override
public void onItemClick(AdapterView<?> adaptr, View arg1, int position, long arg3) {

    Times times = MyXMLHandler.array.get(position);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(times.getDescription());
    builder.setTitle("Description");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            finish();
        }
    });
   builder.show();
}
于 2012-06-20T09:53:45.953 回答
0

你在这里有错误

adapter = new MyAdapter(this,R.layout.list_item);

您的适配器是空的。

于 2012-06-20T09:55:27.697 回答