2015-08-21 56 views
0

我已经通过SO搜索了很多,有几个线程正在讨论这个问题。但实际上没有人帮忙,我用适配器填充了数据,现在我想用行中的一个专用按钮删除一行,其中一个线索有一个建议:在适配器的getView()方法中编写onClickListener 。当我尝试时,出现了一个IndexOutOfBound异常。更准确地说,当我点击第一行的删除按钮时,什么都没有发生,但在第二行和最后一行点击删除按钮给出了例外在ListView中删除一行,IndexOutOfBound异常

这是我试图做的,Android工作室已经建议位置变量应该是最后:

public class TimeTrackerAdapter extends BaseAdapter { 
    public ArrayList<TimeRecord> times=new ArrayList<TimeRecord>(); 
    @Override 
    public int getCount() { 
     return times.size(); 
    } 

    public TimeTrackerAdapter() 
    { 
     times.add(new TimeRecord("12:30","this is the best")); 
     times.add(new TimeRecord("2:30","I need this")); 
    } 
    @Override 
    public Object getItem(int position) { 
     return times.get(position); 
    } 

    public void addTimeRecord(TimeRecord timeRecord) 
    { 
     times.add(timeRecord); 
    } 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View view, ViewGroup parent) { 
     if(view==null) { 
      LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      view = inflater.inflate(R.layout.menu_layout, parent, false); 
     } 
     Button button=(Button) view.findViewById(R.id.delete_entry); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       times.remove(position); 
      } 
     }); 
     TextView timeView=(TextView) view.findViewById(R.id.time_textView); 
     TextView noteView=(TextView) view.findViewById(R.id.note_TextView); 
     TimeRecord time=times.get(position); 
     timeView.setText(time.getTime()); 
     noteView.setText(time.getNote()); 
     return view; 
    } 
} 

和这里的错误堆栈:

08-21 10:47:20.330 20400-20400/com.example.sam.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.sam.myapplication, PID: 20400 
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
     at java.util.ArrayList.remove(ArrayList.java:403) 
     at com.example.sam.myapplication.TimeTrackerAdapter$1.onClick(TimeTrackerAdapter.java:55) 
     at android.view.View.performClick(View.java:4508) 
     at android.view.View$PerformClick.run(View.java:18675) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5584) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
     at dalvik.system.NativeStart.main(Native Method) 
+1

只是删除你的ArrayList中的元素,并刷新您的列表视图 – Dev

+0

@Codebender如果我没看错,你notifyDataSetChange()方法,但我怎么能调用的方法?我不能在onClick方法中实例化另一个对象? –

+0

尝试调用'TimeTrackerAdapter.this.notifyDataSetChanged();' – Codebender

回答

2

您需要在您的按钮添加notifyDataSetChanged()的onClick方法

 Button button=(Button) view.findViewById(R.id.delete_entry); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       times.remove(position); 
       notifyDataSetChanged(); 
      } 
     }); 
1

无论何时添加/删除项目,都应该调用notifyDataSetChanged。这里摘录

public class TimeTrackerAdapter extends BaseAdapter { 
    public ArrayList<TimeRecord> times=new ArrayList<TimeRecord>(); 
    @Override 
    public int getCount() { 
     return times.size(); 
    } 

    public TimeTrackerAdapter() 
    { 
     times.add(new TimeRecord("12:30","this is the best")); 
     times.add(new TimeRecord("2:30","I need this")); 
    } 
    @Override 
    public Object getItem(int position) { 
     return times.get(position); 
    } 

    public void addTimeRecord(TimeRecord timeRecord) 
    { 
     times.add(timeRecord); 
     notifyDataSetChanged(); 
    } 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View view, ViewGroup parent) { 
     if(view==null) { 
      LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      view = inflater.inflate(R.layout.menu_layout, parent, false); 
     } 
     Button button=(Button) view.findViewById(R.id.delete_entry); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       times.remove(position); 
       notifyDataSetChanged(); 
      } 
     }); 
     TextView timeView=(TextView) view.findViewById(R.id.time_textView); 
     TextView noteView=(TextView) view.findViewById(R.id.note_TextView); 
     TimeRecord time=times.get(position); 
     timeView.setText(time.getTime()); 
     noteView.setText(time.getNote()); 
     return view; 
    } 
}