报错: 05-03 23:23:39.106: E/AndroidRuntime(992): FATAL EXCEPTION: main 05-03 23:23:39.106: E/AndroidRuntime(992): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 05-03 23:23:39.106: E/AndroidRuntime(992): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 05-03 23:23:39.106: E/AndroidRuntime(992): at java.util.ArrayList.get(ArrayList.java:311) 05-03 23:23:39.106: E/AndroidRuntime(992): at com.example.snote1.MainActivity$MyAdapter$6.onClick(MainActivity.java:236) 05-03 23:23:39.106: E/AndroidRuntime(992): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159) 05-03 23:23:39.106: E/AndroidRuntime(992): at android.os.Handler.dispatchMessage(Handler.java:99) 05-03 23:23:39.106: E/AndroidRuntime(992): at android.os.Looper.loop(Looper.java:123) 05-03 23:23:39.106: E/AndroidRuntime(992): at android.app.ActivityThread.main(ActivityThread.java:3683) 05-03 23:23:39.106: E/AndroidRuntime(992): at java.lang.reflect.Method.invokeNative(Native Method) 05-03 23:23:39.106: E/AndroidRuntime(992): at java.lang.reflect.Method.invoke(Method.java:507) 05-03 23:23:39.106: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 05-03 23:23:39.106: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-03 23:23:39.106: E/AndroidRuntime(992): at dalvik.system.NativeStart.main(Native Method) 出现数组越界,但是一直找不到哪里越界了。使用了ArrayList,把数据放在ArrayList中,然后通过MyAdapter放在页面中,在删除的过程中,String deleteItemId=(String)mData.get(Position).get(“id”);notesDao.deleteItem(Integer.parseInt(deleteItemId));,想要通过Position获取到我需要删除的数据的那一行的id值,但是一直获取的过来的Position所在位置的下一行的id值。具体可以看看 代码如下! package com.example.snote1; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.integer; import android.app.Activity; import android.app.AlertDialog; import android.app.ListActivity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import com.example.sqlite3.NotesDao; public class MainActivity extends ListActivity{ private static final String TAG="example"; private List<Map<String, Object>> mData; public ImageButton addButton; public ImageButton searchButton; private int oldposition=-1; private Cursor cursor; NotesDao notesDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar); addButton=(ImageButton)findViewById(R.id.addbutton); searchButton=(ImageButton)findViewById(R.id.searchbutton); addButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,NoteEditActivity.class); startActivity(intent); } }); mData=getData(); setListAdapter(new MyAdapter(MainActivity.this)); } @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; } List<Map<String,Object>> getData() { // TODO Auto-generated method stub List<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); notesDao=new NotesDao(this); notesDao.open(); cursor=notesDao.getNotes(); list.clear(); int curnum=cursor.getCount(); if (curnum>0&&cursor!=null) { cursor.moveToFirst(); for (int i = 0; i < curnum; i++) { Map<String, Object> map=new HashMap<String, Object>(); map.put("id", cursor.getString(0)); map.put("title", cursor.getString(1)); map.put("content", cursor.getString(2)); map.put("time", cursor.getString(3)); map.put("bmi", cursor.getString(4)); list.add(map); cursor.moveToNext(); } } cursor.close(); notesDao.close(); return list; } public final class ViewHolder { public TextView title; public TextView content; public TextView time; public ImageButton moreButton; public LinearLayout myLayout; public Button openButton; public Button editButton; public Button deleteButton; public Button renameButton; } public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; public MyAdapter(Context context) { this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return mData.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mData.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.vlist, null); Log.d(TAG, "positon " + position + "converView is null,new "+ convertView); holder.title = (TextView) convertView.findViewById(R.id.title); holder.content = (TextView) convertView.findViewById(R.id.content); holder.time = (TextView) convertView.findViewById(R.id.time); holder.moreButton = (ImageButton) convertView.findViewById(R.id.moreButton); holder.myLayout = (LinearLayout) convertView.findViewById(R.id.mylayout); holder.openButton=(Button)convertView.findViewById(R.id.openbtn); holder.deleteButton=(Button)convertView.findViewById(R.id.deletebtn); holder.editButton=(Button)convertView.findViewById(R.id.editbtn); holder.renameButton=(Button)convertView.findViewById(R.id.renamebtn); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); Log.d(TAG, "positon:" + position + "converView is not null," + convertView); } holder.title.setText((String) mData.get(position).get("id")); holder.content.setText((String) mData.get(position).get("title")); holder.time.setText((String) mData.get(position).get("time")); holder.moreButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MyAdapter.this.getMoreMenu(position); Log.d(TAG, "moreBtn clicked() " + position + " is clicked"); } }); if (position==oldposition) { holder.myLayout.setVisibility(View.VISIBLE); }else { holder.myLayout.setVisibility(View.GONE); } Log.d(TAG, "position:"+position); final int currentPosition=position; holder.openButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,NoteLookActivity.class); //String num=String.valueOf(position); intent.putExtra("result",currentPosition); //setResult(Activity.RESULT_OK, resultIntent); startActivity(intent); } }); holder.editButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); holder.renameButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); holder.deleteButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showMoveDialog(currentPosition); //mData.remove(position); //notifyDataSetChanged(); } }); return convertView; } private void getMoreMenu(int position) { // TODO Auto-generated method stub if (position!=oldposition) { oldposition=position; }else { oldposition=-1; } notifyDataSetChanged(); } private void showMoveDialog(final int currentPosition){ new AlertDialog.Builder(MainActivity.this).setTitle("提示").setMessage("确定要删除吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int newposition=currentPosition-1; mData.remove(currentPosition); //cursor.moveToPosition(currentPosition); Log.d(TAG, "currentPosition "+currentPosition); Log.d(TAG, "listsize "+getData().size()); Log.d(TAG, "deleteItem "+(String)mData.get(currentPosition).get("id")); String deleteItemId=(String)mData.get(currentPosition).get("id"); notesDao.open(); notesDao.deleteItem(Integer.parseInt(deleteItemId)); notifyDataSetChanged(); } }).show(); } } } |
|
40分 |
at com.example.snote1.MainActivity$MyAdapter$6.onClick(MainActivity.java:236)
String deleteItemId=(String)mData.get(currentPosition).get(“id”); 236行对应的就是你这一行,mData中没有数据,size为0, |
你吧mdata强转成字符串了。。。。。当然没有index0的。。。。。你应该加个括号吧
|
|
已经解决。
原来的错误代码。 mData.remove(currentPosition); //cursor.moveToPosition(currentPosition); Log.d(TAG, "currentPosition "+currentPosition); Log.d(TAG, "listsize "+getData().size()); Log.d(TAG, "deleteItem "+(String)mData.get(currentPosition).get("id")); String deleteItemId=(String)mData.get(currentPosition).get("id"); notesDao.open(); notesDao.deleteItem(Integer.parseInt(deleteItemId)); 在mData.remove(currentPosition),把那一行已经remove了。所在下面获取id时出现数组越界。应该先对数据库操作,获取当前位置的position,删除对应的id,然后再对arraylist操作,进行remove。 正确代码 把这句代码mData.remove(currentPosition);放到数据操作之后。 //cursor.moveToPosition(currentPosition); Log.d(TAG, "currentPosition "+currentPosition); Log.d(TAG, "listsize "+getData().size()); Log.d(TAG, "deleteItem "+(String)mData.get(currentPosition).get("id")); String deleteItemId=(String)mData.get(currentPosition).get("id"); notesDao.open(); notesDao.deleteItem(Integer.parseInt(deleteItemId)); mData.remove(currentPosition); |