0

我开始开发 android 应用程序。我创建了一个对话框,可以在其中添加一些文本。当我单击“确定”时,我希望该活动将从 onResume() 或类似的东西开始。在对话框中单击“确定”后,我想更新 ListView。

更新

主要活动:

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.server_list);

}

@Override
protected void onResume() {
    super.onResume();

    //database query and call showServerList

}

public void showServerList(ArrayList<HashMap<String, String>> servers){

    ListView list = (ListView) findViewById(R.id.server_list);

    ListAdapter adapter = new SimpleAdapter( MainActivity.this, servers, R.layout.server_list_entry,
            new String[] {"name"}, new int[]{R.id.server_name});

    list.setAdapter(adapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);        

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_add_server:
            DialogFragment newFragment = new AddServerDialog();
            newFragment.show(getSupportFragmentManager(), "addServer");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

添加服务器对话框:

public class AddServerDialog extends DialogFragment {

View view;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    view = inflater.inflate(R.layout.dialog_add_server, null);

    builder.setView(view)

           .setPositiveButton("Add", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {


                   EditText name = (EditText) view.findViewById(R.id.add_server_name);
                   EditText url = (EditText) view.findViewById(R.id.add_server_address);

                   if(name.getText().toString().isEmpty() || url.getText().toString().isEmpty()){
                       getDialog().cancel();
                   }else{

                       HashMap<String,String> map = new HashMap<String,String>();
                       map.put("name", name.getText().toString());
                       map.put("url", url.getText().toString());
                       //Database query with insert

                   }
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   getDialog().cancel();
               }
           });      
    return builder.create();
}

}

4

1 回答 1

0
    dialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                    //listview.getAdapter().items = newItems...
                    listview.getAdapter().notifyDataSetChanged();
            }
    });
于 2013-07-03T14:08:19.240 回答