2015-08-21 79 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; 
    } 
}