无法使用自定义适配器刷新ListView

时间:2014-05-03 08:45:05

标签: android listview android-listview simplecursoradapter android-cursoradapter

过去两天我一直在工作并弄乱我的代码。

我在片段和按钮中有一个列表视图。 单击按钮时,我将数据添加到通过Dialog输入的列表视图 我无法做的是点击Ok Button后刷新ListView。

这是我的代码

然而,当我退出应用程序并再次恢复时,它会加载

任何帮助将不胜感激

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    int title = getArguments().getInt("title");
    final EditText input = new EditText(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();


return new AlertDialog.Builder(getActivity())
            .setView(input)
            .setIcon(R.drawable.ic_launcher)
            .setTitle(title)
            .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            String Value = input.getText().toString();
                            String normalizednumber = NormalizePhoneUtilities
                                    .normalizePhoneNumber(Value);
                            ContactModel contactModel = new ContactModel(
                                    "Hitesh", normalizednumber, String
                                            .valueOf(3));
                            ContactsDatabaseWorker sm = new ContactsDatabaseWorker(
                                    getActivity());

                            sm.AddSmartContact(contactModel);
                            databaseWorker = new ContactsDatabaseWorker(
                                    getActivity());
                            // LinkedList<Cursor> list = new
                            // LinkedList<Cursor>();
                            // list=databaseWorker.getAllData();

                            run = new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    customAdapter = new CustomCursorAdapter(
                                            getActivity(), databaseWorker
                                                    .getAllData());

                                    customAdapter.notifyDataSetChanged();
                                    listview.invalidateViews();
                                    listview.refreshDrawableState();
                                    listview.setAdapter(customAdapter);
                                }
                            };
                            getActivity().runOnUiThread(run);

                            isModal = false;

                        }

                    })
            .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    }).create();
}






@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (isModal) // AVOID REQUEST FEATURE CRASH
    {
        View view = inflater.inflate(R.layout.smartalert_fragment,
                container, false);
        listview = (ListView) view.findViewById(R.id.lvSmartAlert);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    View view = inflater.inflate(R.layout.smartalert_fragment, container,
            false);
    Button bAdd = (Button) view.findViewById(R.id.bAddContact);
    bAdd.setOnClickListener(this);
    listview = (ListView) view.findViewById(R.id.lvSmartAlert);

    new Handler().post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            customAdapter = new CustomCursorAdapter(getActivity(),
                    databaseWorker.getAllData());
            listview.setAdapter(customAdapter);

        }
    });

    return view;
}

2 个答案:

答案 0 :(得分:0)

在适配器中调用notifyDataSetChanged ();

答案 1 :(得分:0)

您的CursorAdapter有一个swapCursor()方法,每次获得更新的游标时都可以使用该方法。