而不是我的notifydatasetchanged适配器标识的变化(也) [英] My adapter id not changes while notifydatasetchanged() also

查看:204
本文介绍了而不是我的notifydatasetchanged适配器标识的变化(也)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我删除基于列表视图的位置从数组的项目。

  • 我能够从数据库中而不是从列表视图立即删除该项目。

  • 我使用 adapter.notifyDataSetChanged();更新,但它不工作。当使用 adapter.clear()它工作正常,并清除列表视图。
    我删除该数组中的项目基础上,列表视图的位置。

  • 我能够从数据库中而不是从列表视图立即删除该项目。我使用 adapter.notifyDataSetChanged(); 更新,但它不工作。当使用 adapter.clear()它工作正常,并清除列表视图。

  • 我删除基于列表视图的位置从数组的项目。我能够从数据库中而不是从列表视图立即删除该项目。我使用adapter.notifyDataSetChanged();更新,但它不工作。当使用 adapter.clear()它工作正常,并清除列表视图

< - 间隔保持code以下上文所述的项目符号列表中。 - >

  holder.dlttxt.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(查看为arg0){
        // TODO自动生成方法存根
        联系lContact =(联系人)名单
                .getItemAtPosition(位置);
        长positionid = list.getItemIdAtPosition(位置);
        Log.e(,--position ID ---+ positionid);        Log.e(,----删除id--+ lContact.getID()
                +--deleted名字---+ lContact.getName());
        db.deleteContact(lContact.getID());
        权= lContact.getName();
        Toast.makeText(的getContext(),
                lContact.getName()+:+成功地删除,
                Toast.LENGTH_SHORT).show();
        Log.e(,---阵列size--+ imageArry.size());
        imageArry.remove(positionid);
        /*adapter.clear();
        adapter.addAll(db.getAllContacts());
        * /
        adapter.notifyDataSetChanged();
        数= db.getContactsCount();
        HelperActivity.num.setText(将String.valueOf(计数));    }});


解决方案

您可以使用接口办理的点击事件的那删除按钮

code段

接口办理的点击事件

 公共接口ListItemBtnClickInterface {    公共无效listItemBtnClickListener(obj对象,诠释viewId);
}

code代表的构造适配器

 公共AddExpTypeAdapter(上下文的背景下,INT资源,
        清单< ExpenseTypeInfo>对象,ListItemBtnClickInterface _interface){
    超级(上下文,资源,对象);
    // TODO自动生成构造函数存根
    _context =背景;
    layoutId =资源;
    DataList控件=物体;
    this._interface = _interface;
    dbHelper =新PortfolioDbHelper(背景);
    notifyDataSetChanged();
}

code为删除按钮点击适配器初始化后

  vieweHolder.deleteImgView.setTag(位置);
vieweHolder.deleteImgView.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        对象数据= v.getTag();
        如果(数据!= NULL){
            _interface.listItemBtnClickListener(数据,v.getId());
        }
    }
});

code在活动

设置适配器的ListView

  addExpTypeAdapter =新AddExpTypeAdapter(AddExpTypeActivity.this,
                R.layout.row_add_type,expTypeList,新ListItemBtnClickInterface(){                私人诠释clickedPosition;                @覆盖
                公共无效listItemBtnClickListener(obj对象,诠释viewId){
                    // TODO自动生成方法存根
                    clickedPosition =的Integer.parseInt(obj.toString());
                    ExpenseTypeInfo expenseTypeInfo = expTypeList.get(clickedPosition);
                    最终长expTypeId = expenseTypeInfo.getId();
                    开关(viewId){
                        案例R.id.deleteImgView:
                            新AlertDialog.Builder(AddExpTypeActivity.this)
                            .setTitle(getResources()。的getString(R.string.delete_exp_type_title))
                            .setIcon(R.drawable.delete_icon)
                            .setMessage(R.string.delete_exp_type_msg).setCancelable(真)
                            .setPositiveButton(android.R.string.ok,新DialogInterface.OnClickListener(){                            @覆盖
                            公共无效的onClick(DialogInterface对话,诠释它){
                                // TODO自动生成方法存根
                                dbHelper.deleteExpenseType(expTypeId);
                                dbHelper.closeDB();
                                expTypeList.remove(clickedPosition);
                                addExpTypeAdapter.notifyDataSetChanged();
                            }
                        })。setNegativeButton(android.R.string.cancel,新DialogInterface.OnClickListener(){                            @覆盖
                            公共无效的onClick(DialogInterface对话,诠释它){
                                // TODO自动生成方法存根                            }
                        })。显示();
                    打破;                    默认:
                        打破;
                    }
                }
            });
    typeListView.setAdapter(addExpTypeAdapter);

N.B: - 我用 ArrayAdapter 并请换用你的那些变量名

  • I am deleting the item from the array based on the position of the listview.
  • I am able to delete that item from the database but not from the listview immediately.
  • I am using adapter.notifyDataSetChanged(); for updating but it not working. While using adapter.clear() it works fine and clearing the list view. I am deleting the item from the array based on the position of the listview.
  • I am able to delete that item from the database but not from the listview immediately. I am using adapter.notifyDataSetChanged(); for updating but it not working. While using adapter.clear() it works fine and clearing the list view.
  • I am deleting the item from the array based on the position of the listview. I am able to delete that item from the database but not from the listview immediately. I am using adapter.notifyDataSetChanged(); for updating but it not working. While using adapter.clear() it works fine and clearing the list view.

<-- spacer to keep code below out of the bulleted list above. -->

holder.dlttxt.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Contact lContact = (Contact) list
                .getItemAtPosition(position);
        long positionid = list.getItemIdAtPosition(position);
        Log.e("", "--position id---" + positionid);

        Log.e("", "----deleted id--" + lContact.getID()
                + "--deleted name---" + lContact.getName());
        db.deleteContact(lContact.getID());
        quan = lContact.getName();
        Toast.makeText(getContext(),
                lContact.getName() + ":" + "Deleted Sucessfully",
                Toast.LENGTH_SHORT).show();
        Log.e("", "---array size--" + imageArry.size());
        imageArry.remove(positionid);
        /*adapter.clear();
        adapter.addAll(db.getAllContacts());
        */                  
        adapter.notifyDataSetChanged();
        count = db.getContactsCount();
        HelperActivity.num.setText(String.valueOf(count));

    }

});

解决方案

You can use an interface for handling click event of that delete Button

Code snippet

interface for handling click event

public interface ListItemBtnClickInterface {

    public void listItemBtnClickListener(Object obj, int viewId);
}

code for Constructor of Adapter

public AddExpTypeAdapter(Context context, int resource,
        List<ExpenseTypeInfo> objects, ListItemBtnClickInterface _interface) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
    _context = context;
    layoutId = resource;
    dataList = objects;
    this._interface = _interface;
    dbHelper = new PortfolioDbHelper(context);
    notifyDataSetChanged();
}

code for Delete Button click in Adapter after initialization

vieweHolder.deleteImgView.setTag(position);
vieweHolder.deleteImgView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Object data = v.getTag();
        if(data != null) {
            _interface.listItemBtnClickListener(data, v.getId());
        }
    }
});

code for Setting up adapter for ListView in Activity

addExpTypeAdapter = new AddExpTypeAdapter(AddExpTypeActivity.this, 
                R.layout.row_add_type, expTypeList, new ListItemBtnClickInterface() {

                private int clickedPosition;

                @Override
                public void listItemBtnClickListener(Object obj, int viewId) {
                    // TODO Auto-generated method stub
                    clickedPosition = Integer.parseInt(obj.toString());
                    ExpenseTypeInfo expenseTypeInfo = expTypeList.get(clickedPosition);
                    final long expTypeId = expenseTypeInfo.getId();
                    switch (viewId) {
                        case R.id.deleteImgView:
                            new AlertDialog.Builder(AddExpTypeActivity.this)
                            .setTitle(getResources().getString(R.string.delete_exp_type_title))
                            .setIcon(R.drawable.delete_icon)
                            .setMessage(R.string.delete_exp_type_msg).setCancelable(true)
                            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                dbHelper.deleteExpenseType(expTypeId);
                                dbHelper.closeDB();
                                expTypeList.remove(clickedPosition);
                                addExpTypeAdapter.notifyDataSetChanged();
                            }
                        }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub

                            }
                        }).show();
                    break;

                    default:
                        break;
                    }
                }
            });
    typeListView.setAdapter(addExpTypeAdapter);

N.B :- I'm using ArrayAdapter and please change those variable name with yours

这篇关于而不是我的notifydatasetchanged适配器标识的变化(也)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆