2012-08-17 81 views
5

我不明白什麼是錯notifyDataSetChanged() - IndexOutOfBoundException

我使用

public class UrlArrayAdapter extends BaseAdapter {...  
     ArrayList<UrlItem> objects; 

     UrlArrayAdapter(Context context, ListView urlListView, 
       ArrayList<UrlItem> urlLists) { 
objects = urlLists; ... 



    //method 
    public void deleteItem(int numberToDelete) {   
      objects.remove(numberToDelete);  
      notifyDataSetChanged(); 
     } 

AND(最有趣的)

我得到numberToDelete = 1

這條線被刪除

enter image description here

但對象 enter image description here

並且當numberToDelete = 0

此行然後除去

enter image description here

但對象 enter image description here

並且當numberToDelete = 2

對象

enter image description here

但notifyDataSetChanged後(); IndexOutOfBoundException

我已經有一個帖子...但我解決不了這個問題

IndexOutOfBoundException when I use notifyDataSetChanged

stackoverflow.com/a/9260757/1568164我覺得情況是這樣的,我用但不行

然後我得到一個位置,以進一步去除

public View getView(int position, View convertView, ViewGroup parent) { 

     // Planet to display 
     UrlItem urlItem = (UrlItem) this.getItem(position); 
     ViewHolder viewHolder = null; 

     if (convertView == null) { 

      convertView = lInflater.inflate(R.layout.database_table_item, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.checkbox = (CheckBox) convertView 
        .findViewById(R.id.checkBox1); 

      viewHolder.checkbox.setOnCheckedChangeListener(listener1); 

      viewHolder.text = (EditText) convertView 
        .findViewById(R.id.editText1); 


      viewHolder.text 
        .setOnFocusChangeListener(new OnFocusChangeListener() { 
         public void onFocusChange(View v, boolean hasFocus) { 
          int getPosition = (Integer) v.getTag(); 
          if (!hasFocus) { 

           final EditText Caption = (EditText) v; 
           // urlLists 
           objects.get(getPosition).setUrl(
             Caption.getText().toString()); 
          } else { 

           DatabaseTable.setPosition(getPosition); // This is a future for "numberToDelete" 
          } 
         } 
        }); 

      convertView.setTag(viewHolder); 
      convertView.setTag(R.id.checkBox1, viewHolder.checkbox); 
      convertView.setTag(R.id.editText1, viewHolder.text); 

     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 

     } 
     viewHolder.text.setTag(position); 
     viewHolder.text.setText(urlItem.getUrl()); 
     viewHolder.checkbox.setTag(position); 
     viewHolder.checkbox.setChecked(urlItem.getUse()); 

     return convertView; 

    } 

我的基礎活動,其中我打電話移除管路

public class DatabaseTable extends Activity { 
    private Settings setting; 
    private ArrayList<UrlItem> urlLists; 
    private ListAdapter uAdapter; 
    private ListView urlListView; 
    static int position; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.database_table_list); 
     LBD conection = new LBD(this); 
     conection.open(); 
     setting = conection.tblSettings(); 
     setting.create(); 
     urlLists = setting.selectRssTable(); 
     urlListView = (ListView) findViewById(R.id.listView1); 
     uAdapter = new UrlArrayAdapter(DatabaseTable.this, urlListView, 
       urlLists); 
     urlListView.setAdapter(uAdapter); 
     urlListView.setItemsCanFocus(true); 
} 
    public static void setPosition(int pos) { 
     position = pos; 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     menu.add(Menu.NONE, 2, 2, "Delete URL"); 
     return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case 2: 
     ((UrlArrayAdapter) uAdapter).deleteItem(position); 
break; 

     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

繼續我試驗,得出的結論是,在適配器列表中的字符串關係不大的事實,輸出到屏幕上,這樣的挑戰是改變主題 。需要了解如何從屏幕上刪除行

+0

可以爲您的代碼添加到您的問題在當你從表中選擇一個對象返回該對象的索引? – 2012-08-17 08:50:53

+0

我添加了請看 – 2012-08-17 09:00:01

+0

'int position'是什麼,這是所選UrlItem的索引嗎? – 2012-08-17 09:00:59

回答

2

該解決方案很簡單。

viewHolder.text 
        .setOnFocusChangeListener(new OnFocusChangeListener() { 
         public void onFocusChange(View v, boolean hasFocus) { 

沒有正常工作,重繪焦點名單已經改變和刪除線混亂..苦惱了一個星期的決定很簡單,留給修復