当我从自定义对话框中解除时,如何在父片段中更新我的Listview

时间:2016-05-26 12:22:10

标签: android customdialog

我有一个listview setonlongclicklistner来打开一个显示删除和取消的自定义对话框。

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


            Cursor c = (Cursor) sp.getItem(position);
            int c_id = c.getInt(0);
            String userid = c.getString(1) + c_id;

            frag = new UserListDeleteFragment();

            Bundle bundle = new Bundle();
            bundle.putString("id", "" + c_id);
            bundle.putString("userid", userid);
            frag.setArguments(bundle);
            frag.setCancelable(false);

            frag.show(Lenderlist.this.getSupportFragmentManager(), "f");

            return true;
        }
    }); 

以下是自定义对话框的代码,该对话框将删除所选列表项

 public class UserListDeleteFragment extends DialogFragment {

    TextView tv1,tv2;
    Dialog d;
    String user_id,ids;
    MyDatabase m;


    public UserListDeleteFragment() {
        // Required empty public constructor
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        d=null;
        m=new MyDatabase(getActivity());

        AlertDialog.Builder ab=new AlertDialog.Builder(getActivity());

        View v=getActivity().getLayoutInflater().inflate(R.layout.fragment_user_list_delete, null);

        tv1= (TextView) v.findViewById(R.id.textviewbutton1);
        tv2= (TextView) v.findViewById(R.id.textviewbutton2);

        Bundle b=getArguments();
        user_id = getArguments().getString("userid");
        ids=getArguments().getString("id");


        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean b;
                m.open();
                b=m.deleteTitle(ids);
                m.close();
                m.open();
                m.deletesublist_user(user_id);
                m.close();

                if(b==true)
                {
                    Toast.makeText(getActivity(),"Deleted",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(getActivity(),"Not Deleted",Toast.LENGTH_SHORT).show();
                }


                d.dismiss();

            }
        });

        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                d.dismiss();
            }
        });


        ab.setView(v);
        d=ab.create();

        return d;
    }
    }

我希望在对话框解除时从第一个片段更新我的列表视图。任何人都有解决方案。我不擅长android。也很抱歉英语不好。提前谢谢。

1 个答案:

答案 0 :(得分:0)

覆盖UserListDeleteFragment的onDismiss方法,以调用列表适配器的notifyDataSetChanged