在AlertDialog关闭时刷新片段视图

时间:2013-06-13 20:03:51

标签: android android-fragments alertdialog

在确认AlertDialog之后,我在DB上调用方法。 此方法删除项目表单列表,现在我想刷新片段,因此已删除的项目不再在列表中。

public class My extends Fragment implements Receiver{
    View view;
    TextView lastSync;

    private String inputNum;

    private ListView myListView;
    static final String KEY_ID = "Id mjerenja";
    static final String KEY_BROJILO = "Brojilo";
    static final String KEY_ENERGENT = "Energent";
    static final String KEY_ADRESA = "Adresa";
    static final String KEY_VRIJEME = "Vrijeme";
    static final String KEY_STANJE = "Stanje";

    ResultReceiverFromIS mReceiver = new ResultReceiverFromIS(new Handler());

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = (LinearLayout) inflater.inflate(R.layout.my, container, false);
        lastSync = (TextView) view.findViewById(R.id.myLastSync);
        mReceiver.setReceiver(this);

        DatabaseHandler dbH = new DatabaseHandler(getActivity());
        lastSync.setText(dbH.GetLastSync());
        List<NewJobs> mesList = dbH.getMyMeasurements();
        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        for (NewJobs newJobs : mesList) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(KEY_ID, String.valueOf(newJobs.getSifMjerenja()));
            map.put(KEY_BROJILO, String.valueOf(newJobs.getSifBrojila()));
            map.put(KEY_ADRESA, newJobs.getAdresa()+", "+newJobs.getNazMjesto());
            map.put(KEY_ENERGENT, newJobs.getNazEnergent());
            map.put(KEY_VRIJEME, newJobs.getDatum());
            map.put(KEY_STANJE, String.valueOf(newJobs.getStanje()));
            menuItems.add(map);
        }
        myListView = (ListView) view.findViewById(R.id.myListView);
        ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
                R.layout.my_list_item, new String[] { KEY_ID,KEY_BROJILO, KEY_ENERGENT,
                        KEY_ADRESA, KEY_VRIJEME, KEY_STANJE}, new int[] { R.id.idmjerenje, R.id.idbrojilo,
                        R.id.energent, R.id.adresa, R.id.datum, R.id.stanje });
        myListView.setAdapter(adapter);

        myListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                final String idmjerenje = ((TextView) view.findViewById(R.id.idmjerenje)).getText().toString();
                String idbrojilo = ((TextView) view.findViewById(R.id.idbrojilo)).getText().toString();
                String energent = ((TextView) view.findViewById(R.id.energent)).getText().toString();
                String adresa = ((TextView) view.findViewById(R.id.adresa)).getText().toString();
                String vrijeme = ((TextView) view.findViewById(R.id.datum)).getText().toString();
                String stanje = ((TextView) view.findViewById(R.id.stanje)).getText().toString();

                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("Šifra brojila: "+idbrojilo+"\n"+"Energent: "+energent+"\n"+adresa+"\n"+"Datum: "+vrijeme+"\n"+"Uneseno stanje: "+stanje);
                builder.setTitle("Unos mjerenja:");
                final EditText textEdit = new EditText(getActivity());
                textEdit.setId(1);
                textEdit.setHint("Novo stanje");
                textEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
                textEdit.setGravity(Gravity.LEFT);
                builder.setView(textEdit);
                builder.setIcon(R.drawable.popup);
                builder.setPositiveButton("U redu", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        inputNum = textEdit.getText().toString();
                        if (!inputNum.isEmpty()) {
                            AlertDialog.Builder builder2 = new AlertDialog.Builder(getActivity());
                            builder2.setTitle("Unos mjerenja:");
                            builder2.setIcon(R.drawable.popup);
                            builder2.setMessage("Novo stanje: "+inputNum+ "\n\n"+"Želite li potvrditi?");
                            builder2.setPositiveButton("Potvrdi", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) {
                                      DatabaseHandler dbH = new DatabaseHandler(getActivity());                            
                                        dbH.updateJob(idmjerenje,inputNum);


                                   }
                                });
                            builder2.setNegativeButton("Odustani", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {
                                   dialog.cancel();
                               }
                            });
                            builder2.create().show();                           
                        }
                    }
                });
                builder.setNegativeButton("Odustani", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                });
                builder.create().show();
            }
        });


        return view;
    }

我试图谷歌搜索并搜索stackoverflow但没有运气。

任何人都可以提供帮助或提供建议吗?

THX

编辑:

当我添加:

List<NewJobs> mesList = dbH.getMyMeasurements();
                                        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

                                        for (NewJobs newJobs : mesList) {
                                            HashMap<String, String> map = new HashMap<String, String>();
                                            map.put(KEY_ID, String.valueOf(newJobs.getSifMjerenja()));
                                            map.put(KEY_BROJILO, String.valueOf(newJobs.getSifBrojila()));
                                            map.put(KEY_ADRESA, newJobs.getAdresa()+", "+newJobs.getNazMjesto());
                                            map.put(KEY_ENERGENT, newJobs.getNazEnergent());
                                            map.put(KEY_VRIJEME, newJobs.getDatum());
                                            map.put(KEY_STANJE, String.valueOf(newJobs.getStanje()));
                                            menuItems.add(map);
                                        }
                                        adapter = new SimpleAdapter(getActivity(), menuItems,
                                                R.layout.my_list_item, new String[] { KEY_ID,KEY_BROJILO, KEY_ENERGENT,
                                                        KEY_ADRESA, KEY_VRIJEME, KEY_STANJE}, new int[] { R.id.idmjerenje, R.id.idbrojilo,
                                                        R.id.energent, R.id.adresa, R.id.datum, R.id.stanje });
                                        myListView.setAdapter(adapter);

之后:

DatabaseHandler dbH = new DatabaseHandler(getActivity());                            
                                        dbH.updateJob(idmjerenje,inputNum);

在第二个AlertDialog中,它正常刷新。

我可以更优雅地做到这一点吗?

1 个答案:

答案 0 :(得分:0)

onResume中,只需致电adapter.notifyDatasetChanged()即可刷新您的列表