2

在 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上搜索,但没有运气。

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

谢谢

编辑:

当我添加:

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 中,它会正常刷新。

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

4

1 回答 1

0

在您onResume刚刚的电话adapter.notifyDatasetChanged()中,这将刷新您的列表

于 2013-06-13T20:05:45.563 回答