notifyDataSetChanged() - IndexOutOfBoundException

时间:2012-08-17 08:44:42

标签: android listview baseadapter indexoutofboundsexception

我不明白出了什么问题

我正在使用

  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

AND 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);
    }

}

继续主题 我试验并得出结论,适配器列表中的字符串与输出到屏幕的事实几乎没有关系,因此挑战正在发生变化。需要了解如何从屏幕上删除行

1 个答案:

答案 0 :(得分:2)

解决方案很简单。

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

没有正常工作,重绘焦点列表已经改变,删除线条混乱..痛苦一周的决定很简单,留下来修复