从另一个类访问整数值?

时间:2018-01-08 05:21:21

标签: java android

package com.efftronics.android.eEmployee.Activity.Installation;

public class InstActivityDialog extends Dialog {
    //CustomAutoCompleteTextView actvInstallationActivities;
    EditText etInstActvs;
    Spinner spInstStatus;
    EditText etInstSpentHrs, etReworkReason;
    Button btnAdd, btnCancel;
    EEmployeeApplication mContext;
    ArrayAdapter<String> statusAdapter, InstActivityAdapter;
    Calendar calendar = Calendar.getInstance();
    //Work_Status_Entry Work_Status; COMMENTED BY PAVANI
    com.efftronics.android.eEmployee.Activity.Installation.Inst_Plan_Details Inst_Plan_Details;
    InstallationData currInstallationData;
    INST inst;
    FloatingActionButton fab_activity,fab_img;
    int pos,iAID,iPID;
    String iActvity;
    public int Activity_or_Fragment=0;//added by pavani
    public int Add_or_Update=0;
    Entry_Fragment entry_fragment;//added by pavani

    private EmpConstants.OperationType TypeOfOperation;

    public InstActivityDialog(Context context, com.efftronics.android.eEmployee.Activity.Installation.Inst_Plan_Details inst_plan_details, int position, String instaActivity, int instaAID, int instaPID) {
        super(context);
        Inst_Plan_Details = inst_plan_details;
        mContext = (EEmployeeApplication) context.getApplicationContext();
        pos = position;
        iActvity = instaActivity;
        iAID = instaAID;
        iPID = instaPID;
        Activity_or_Fragment=1;//added by pavani

    }

    //Added by Pavani
    public InstActivityDialog(Context context,Entry_Fragment aEntry_fragment) {
        super(context);
        entry_fragment = aEntry_fragment;
        mContext = (EEmployeeApplication) context.getApplicationContext();
        Activity_or_Fragment=2;

    }



//    public InstActivityDialog(Context context, Work_Status_Entry work_status_entry) {
//        super(context);
//        Work_Status = work_status_entry;
//        mContext = (EEmployeeApplication) context.getApplicationContext();
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            setContentView(R.layout.installation_dialog); //layout for dialog
            setTitle("Installation Activity");
            setCancelable(false);

            //InstallationsArrayList = new ArrayList<>();

            currInstallationData = new InstallationData();
            // set the custom dialog components
            TypeOfOperation = EmpConstants.OperationType.add;
            etInstActvs = (EditText) findViewById(R.id.etInstaActivities);
            spInstStatus = (Spinner) findViewById(R.id.SpInstaStatus);
            etInstSpentHrs = (EditText) findViewById(R.id.etInstaSpentHrs);
            etReworkReason = (EditText) findViewById(R.id.edReworkReason);
//            etReworkReason.setVisibility(View.GONE); // Hiding Temporarily
            //InstallationsArrayList = new ArrayList<>();
            // ADD and CANCEL buttons in adding new Installation Entry
            btnAdd = (Button) findViewById(R.id.btn_instal_ok);
            btnCancel = (Button) findViewById(R.id.btn_instal_cancel);
            btnAdd.setOnClickListener(onAddorUpdateConfirmListener(this));
            btnCancel.setOnClickListener(onCancelListener());
            if( Activity_or_Fragment==1){
                etInstActvs.setEnabled(false);
                etInstActvs.setInputType(InputType.TYPE_NULL);
                etInstActvs.setFocusable(false);
            }
            else if( Activity_or_Fragment==2)
            {
                etInstActvs.setEnabled(true);
                etInstActvs.setFocusable(true);
            }
            /*etInstActvs.setEnabled(true);
            //etInstActvs.setInputType(InputType.TYPE_NULL);
            etInstActvs.setFocusable(true);*/
            etInstActvs.setText(iActvity);
//            etInstActvs.setText(currInstallationData.instaActivity);
//            InstActivityAdapter.getItem()


            // Adding Listeners
            etInstSpentHrs.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        setTime();
                    }
                }
            });

            etInstSpentHrs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTime();
                    etInstSpentHrs.setText(mContext.statusTimeFmt.format(calendar.getTime()));
                }
            });

            // Creating Adapters
            String[] StatusTypes = mContext.getResources().getStringArray(R.array.InstStatusTypes);
            statusAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,StatusTypes);
            spInstStatus.setAdapter(statusAdapter);

            // Installation Activities Entry Field

        }catch (Exception e){
            Log.e("eEmp/InstDlg","Excp due to "+e.toString());
        }
    }

    // Change Button Text based on the Type of Operation
    public void setTypeofOperation(EmpConstants.OperationType aTypeofOperation) {
        TypeOfOperation = aTypeofOperation;
        if (TypeOfOperation == EmpConstants.OperationType.add) {
            btnAdd.setText("ADD");

        } else {
            btnAdd.setText("UPDATE");
        }
    }

    // Set Time Picker Dialog Time to Current Time
    TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calendar.set(Calendar.MINUTE, minute);
            etInstSpentHrs.setText(mContext.statusTimeFmt.format(calendar.getTime()));
        }
    };

    // Assign Updated Time to Installation Spent Hrs Filed
    public void setTime() {
        try {
            new TimePickerDialog(getContext(), t,00,00, true).show();
            etInstSpentHrs.setText(mContext.statusTimeFmt.format(calendar.getTime()));
        } catch (Exception timePickerDlgExcpt) {
            Log.d("eEmp/TimePickerDlg", "Exception Occurred due to " + timePickerDlgExcpt.toString());
        }
    }
    // Click Listener for ADD/UPDATE button
    private View.OnClickListener onAddorUpdateConfirmListener(final InstActivityDialog aCurDialog) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Activity_or_Fragment== 1)
                {
                    try {
                        String toastStr = ValidateInstallationInputData();

                        if (toastStr.isEmpty()) {
                            if (TypeOfOperation == EmpConstants.OperationType.add) {
                                currInstallationData = new InstallationData();
                                inst = new INST();
                                Add_or_Update=1;//added by pavani

                            }
                            currInstallationData.instaActivity = iActvity;
                            currInstallationData.instaStatus = spInstStatus.getSelectedItemPosition();
                            currInstallationData.instaTime = etInstSpentHrs.getText().toString();
                            currInstallationData.instaReason = etReworkReason.getText().toString();
                            currInstallationData.currDialog = aCurDialog;
                            inst.AID = iAID;
                            inst.REASON_OF_REWORK = etReworkReason.getText().toString();
                            inst.INST_PLAN_ID = iPID;

                            String spntHrsStr = etInstSpentHrs.getText().toString().trim();
                            int hour = Integer.parseInt(spntHrsStr.substring(0, 2));
                            int mints = Integer.parseInt(spntHrsStr.substring(3, 5));
                            int spntHrs = (hour * 60) + mints;
                            inst.SHR = spntHrs;

                            inst.INST_STATUS = spInstStatus.getSelectedItemPosition();

                            if (TypeOfOperation == EmpConstants.OperationType.add) {

//                            Inst_Plan_Details.Inst_List_main.add(currInstallationData);
                                int curpos = pos;
                                if (Inst_Plan_Details.Inst_List_main.get(curpos).instaAID == iAID) {
                                    //   Inst_Plan_Details.Inst_List_main.set(curpos,currInstallationData);
                                    Inst_Plan_Details.Inst_List_main.remove(curpos);
                                    Inst_Plan_Details.Inst_List_main.add(curpos, currInstallationData);
                                    Inst_Plan_Details.Inst_Act_List.add(inst);
                                }
                            }

                            Inst_Plan_Details.inst_activity_adapter.notifyDataSetChanged();


//                        //Hiding mobile soft keyboard on Login button click
//                        InputMethodManager inputmethod = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
//                        inputmethod.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

                            //close dialog after all
                            dismiss();

                            // Check if no view has focus:
                            //TODO: to be close Keyboard after close
                            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                        } else {
                            Snackbar.make(v, toastStr, Snackbar.LENGTH_LONG).show();
                        }


                    } catch (Exception e) {
                        Log.d("eEmp/NewInstActAdd", "Exception occurred due to " + e.toString());
                    }
            }

            //Added by pavani
            else if(Activity_or_Fragment==2){
                    try {
                        String toastStr = ValidateInstallationInputData();
                        if (toastStr.isEmpty()) {
                            if (TypeOfOperation == EmpConstants.OperationType.add) {
                                currInstallationData = new InstallationData();
                                inst = new INST();
                                Add_or_Update=2;
                            }

                            currInstallationData.instaActivity = etInstActvs.getText().toString();
                            currInstallationData.instaStatus = spInstStatus.getSelectedItemPosition();
                            currInstallationData.instaTime = etInstSpentHrs.getText().toString();
                            currInstallationData.instaReason = etReworkReason.getText().toString();
                            currInstallationData.currDialog = aCurDialog;

                           /* currInstallationData.selectedActivityObj =
                                    ((IActivity) (selectedInstActivity).childObject);*/

                            // Calculate in Minutes
                            String spntHrsStr = etInstSpentHrs.getText().toString().trim();
                            int hour = Integer.parseInt(spntHrsStr.substring(0, 2));
                            int mints = Integer.parseInt(spntHrsStr.substring(3, 5));
                            int spntHrs = (hour * 60) + mints;
                            inst.SHR = spntHrs;


                            if (TypeOfOperation == EmpConstants.OperationType.add) {
                                entry_fragment.InstallationsList.add(currInstallationData);
                                // Add to Info List for Entry
                                entry_fragment.Inst_Act_List_works_entry.add(inst);
                            }

                            //notify data set changed in RecyclerView adapter
                            entry_fragment.InstActivityAdapter.notifyDataSetChanged();

                            //Hiding mobile soft keyboard on Login button click
                            InputMethodManager inputmethod = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
                            inputmethod.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

                            //close dialog after all
                            dismiss();

                            // Check if no view has focus:
                            //TODO: to be close Keyboard after close
                            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

                        }
                        else{
                            Snackbar.make(v, toastStr, Snackbar.LENGTH_LONG).show();
                            }
                    }//try ended here
                    catch (Exception e) {
                        Log.d("eEmp/NewInstActAdd", "Exception occurred due to " + e.toString());
                    }//catch ended here
                }//elseif ended here
            }
        };
    }//method ended here

    private String ValidateInstallationInputData(){
        String toastStr = "";
        /*if (iActvity == null) {
            if (actvInstallationActivities.getText().length() == 0) {
                toastStr = "Please select Activity Name";
            } else {
                toastStr = "Please select Proper Activity Name";
            }
            //     actvInstallationActivities.requestFocus();
        } else if (spInstStatus.getSelectedItemPosition() < 0) {
            toastStr = "Please choose Work Status";
            //      spInstStatus.requestFocus();
        } else if (etInstSpentHrs.getText().toString().isEmpty()) {
            toastStr = "Please enter Spent Hours";
            //     etInstSpentHrs.requestFocus();
        }*/
        if (spInstStatus.getSelectedItemPosition() < 0){
            toastStr = "Please select Work Status";
            spInstStatus.requestFocus();
        }else if (etInstSpentHrs.getText().toString().isEmpty()){
            toastStr = "Please enter Spent Hrs";
            etInstSpentHrs.requestFocus();
        }
//        else if (etReworkReason.getText().toString().isEmpty()){
//            toastStr = "Please enter Reason Of Work";
//            etReworkReason.requestFocus();
//        }
        return toastStr;
    }
    // Cancel Button Click Listener
    private View.OnClickListener onCancelListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        };
    }
}

这是另一堂课。

package com.efftronics.android.eEmployee.Activity.Installation;
public class Inst_Activity_Adapter extends RecyclerView.Adapter<Inst_Activity_Adapter.ViewHolder> {
    private ArrayList<InstallationData> installationsList;
    private Activity activity;
    EEmployeeApplication mContext;

    @Override
    public Inst_Activity_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        try {
                //inflate your layout and pass it to view holder
                LayoutInflater inflater = activity.getLayoutInflater();

                final View view = inflater.inflate(R.layout.installation_item_card, parent, false);
                viewHolder = new ViewHolder(view);
//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(view.getContext(),"pos",Toast.LENGTH_LONG).show();
//                }
//            });
                return viewHolder;
        } catch (Exception e) {
            e.printStackTrace();
            return viewHolder;
        }
    }
    public Inst_Activity_Adapter(Activity activity, ArrayList<InstallationData> inst_actvs){
        this.installationsList = inst_actvs;
        this.activity = activity;
    }

    @Override
    public void onBindViewHolder(Inst_Activity_Adapter.ViewHolder viewHolder, int position) {
//setting data to view holder elements
        Log.d("eEMP/InstRv", "Pos=" + position);
        viewHolder.VH_Activity.setText(installationsList.get(position).instaActivity);
        viewHolder.VH_Time.setText(installationsList.get(position).instaTime);
         viewHolder.VH_Reason.setText(installationsList.get(position).instaReason); // Hiding Reason for Rework

        if (installationsList.get(position).instaStatus == 1) {
            viewHolder.imageView.setImageResource(R.mipmap.inprog_icon); // R.drawable.stat_complete
            viewHolder.VH_Status.setText(R.string.Inprogress_status); //@string/StatusCompleted
        } else if (installationsList.get(position).instaStatus == 0) {
            viewHolder.imageView.setImageResource(R.mipmap.complete_icon); // R.drawable.stat_inprog
            viewHolder.VH_Status.setText(R.string.completed_status);// @string/StatusInProgress
        } else if (installationsList.get(position).instaStatus == 2) {
            viewHolder.imageView.setImageResource(R.mipmap.incomplete_icon); // R.drawable.stat_incomplete
            viewHolder.VH_Status.setText(R.string.Incompleted_status);//@string/StatusInCompleted
        }
        //set on click listener for each element
        viewHolder.container.setOnClickListener(onClickListener(position));
    }

    private View.OnClickListener onClickListener(final int position) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if(Activity_or_Fragment==1 && Add_or_Update==1) {
//                    installationsList.get(position).currDialog.setTypeofOperation
//                            (EmpConstants.OperationType.add);
//                    installationsList.get(position).currDialog.show();
                        Inst_Plan_Details plan = new Inst_Plan_Details();
                        plan = (Inst_Plan_Details) activity;
                        InstActivityDialog dlg = new InstActivityDialog(activity, plan, position, installationsList.get(position).instaActivity, installationsList.get(position).instaAID, installationsList.get(position).instPID);
                        dlg.show();
                    }
                    else if(Activity_or_Fragment==2 &&  Add_or_Update==2){
                        installationsList.get(position).currDialog.setTypeofOperation
                                (EmpConstants.OperationType.update);
                        installationsList.get(position).currDialog.show();

                    }
                }catch (Exception e){
                    Log.e("eEmp/Inst_Adapter","Excp due to"+e.toString());
                }
//                Log.e("eEmp/Inst_Adapter","Test");
            }
        };
    }
    @Override
    public int getItemCount() {
        return installationsList.size();
        //return (null != installationsList ? installationsList.size() : 0);
    }

    protected class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView VH_Activity;
        private TextView VH_Status;
        private TextView VH_Time;
        private TextView VH_Reason;
        private View container;
        public ViewHolder(View view) {
            super(view);
            try{
                imageView = (ImageView) view.findViewById(R.id.ivInstaStatus);
                VH_Activity = (TextView) view.findViewById(R.id.tvActivityName);
                VH_Status = (TextView) view.findViewById(R.id.tvActivityStatus);
                VH_Time = (TextView) view.findViewById(R.id.tvActivityTime);
                VH_Reason = (TextView) view.findViewById(R.id.tvActivityReason);
                VH_Reason.setVisibility(View.GONE);
                container = view.findViewById(R.id.installation_card_view);

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

InstActivityDialog中,有2个变量int Activity_or_Fragmentint Add_or_Update

我在这个课程中使用了这些值。现在依赖于我需要在onClickListener Inst_Activity_Adapter class中执行操作所需的这些值。

因此,我需要在onClick类的Inst_Activity_Adapter方法中访问这些变量。我该如何访问它们?

我将这些变量设为公开并尝试访问if(Activity_or_Fragment==1 && Add_or_Update==1),但它给出了错误。

我如何访问它们?

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

尝试制作你的int变量static

或使用constructor概念

例如

class First{
    public static int value = 1;
}

class Second
{
    System.out.println(""+firstClass.value);
}

注意: 声明任何变量静态 请先参阅https://r.je/static-methods-bad-practice.html

<强> https://dzone.com/articles/why-static-bad-and-how-avoid

答案 1 :(得分:0)

您可以将该值作为参数发送到其他类中,也可以将该值设置为静态。 :)