我有一个关于从listView中删除项目的查询,并通知android中的更改

时间:2016-04-12 12:35:09

标签: android listview android-fragments

我必须从listView中删除项目在android中并通知其更改。我从StackOverflow获得了一些解决方案,但无法使其正常工作。

这部分代码显示了我如何从DAO类初始化列表视图和调用函数。

listvcard=(ListView) rootView.findViewById(R.id.listofvcard);

         DAO dao = new DAO(getActivity());

         vcardadapter adapter = new vcardadapter(getActivity(), dao.getUser());

        listvcard.setAdapter(adapter);

        return rootView;
    }

    private class vcardadapter extends BaseAdapter {

        private Context context;
        private List<VcardVO> listvcardVO;


        private LayoutInflater inflater;

        public vcardadapter(Context c, List<VcardVO> list) {
            // TODO Auto-generated constructor stub
            this.context = c;
            this.listvcardVO = list;
            inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

=============================================== =============================

这部分代码显示了如何使用ALertlayout从listView中删除Item。

h.delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    deletepopup();
                }
                private void deletepopup() {
                    // TODO Auto-generated method stub
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("Delete this vCard?");

                    alert.setCancelable(false);
                    //alert.setIcon(R.drawable.vcardicon2);
                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

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

                        }
                    });
                    alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {

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


                            ArrayAdapter<String> myAdapter = (ArrayAdapter<String>)listvcard.getAdapter();
                            myAdapter.remove(myAdapter.getItem(position));
                            myAdapter.notifyDataSetChanged();



                        }
                    });
                    AlertDialog dialog = alert.create();
                    dialog.show();
                }
            });

我在第"ArrayAdapter<String> myAdapter = (ArrayAdapter<String>)listvcard.getAdapter();"

中收到错误

它说&#34; vcardadapter无法转换为arrayadapter&#34;

请回复!!我试图从3天后解决这个问题。

1 个答案:

答案 0 :(得分:0)

试试这个,

添加一个方法,从下面给出的适配器中的listview中删除项目:

public removeItem(int position){
         listvcardVO.remove(position);
         notifyDataSetChanged();
}

并从删除按钮调用方法单击:<​​/ p>

adapter.removeItem(postion);