Wednesday, March 9, 2016

Custom Dialog that access its own UI elements

public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
case RENAME_ID:

final MenuItem itemF = item;
AlertDialog.Builder renameDialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_group_rename, null);
renameDialogBuilder.setView(dialogView);
final View dialogViewF = dialogView;

renameDialogBuilder.setTitle(getString(R.string.rename_group)).

        setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Log.d(P.Tag, "OK clicked");

                EditText groupName = (EditText)dialogViewF.findViewById(R.id.group_rename);
                String groupNameStr = groupName.getText().toString();
                if (groupNameStr.length() < 5) {
                    Toast.makeText(getActivity().getApplicationContext(), getString(R.string.groupname_invalid), Toast.LENGTH_LONG).show();
                    return;
                }


                AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo)itemF.getMenuInfo();
                Log.d(P.TAG, "Renaming ID: " + info.id);
                int position = (new Long(info.id)).intValue();
                try {
                    String groupId = ((JSONObject)jsonGroupArray.get(position)).get("id").toString();
                    String adminId = ((JSONObject)jsonGroupArray.get(position)).get("admin_id").toString();
                    String userId = P.getUserId(getActivity().getApplicationContext());
                    if (userId.equals(adminId)) {
                        Log.d(P.TAG, "Renaming groupId: " + groupId);
                        new HttpCallAsyncTask(getActivity()).execute("oper", "rename_group", "user_id", userId, "group_id", groupId, "group_name", groupNameStr);
                    }


                } catch (JSONException e) {
                    Log.e(P.TAG, "JSONException when trying to leave position: " + position );
                    Toast.makeText(getActivity().getApplicationContext(), getString(R.string.delete_failed), Toast.LENGTH_LONG).show();
                }


            }
        }).
        setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Log.i(P.TAG, "cancel clicked, doing nothing");
            }
        });
AlertDialog alert  = renameDialogBuilder.create();
alert.show();

No comments:

Post a Comment