我的get方法即使有值也返回0

时间:2019-08-13 18:29:53

标签: java android getter-setter

所以我有两个课。我正在尝试使用get方法(getInputTime)将整数数据从一个类发送到另一个类。我在get方法中返回的变量具有一个值。但是,当我在另一个名为TimeActivity的类中使用get方法时,它只会返回0。


public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

    private static final String TAG = "CustomAdapter";
    private ArrayList<Integer> mWorkTW = new ArrayList<>();
    private ArrayList<Integer> mWorkET = new ArrayList<>();
    private ArrayList<Integer> mRestTW = new ArrayList<>();
    private ArrayList<Integer> mRestET = new ArrayList<>();
    private Context mContext;
    private int numberOfIntervals;

    public CustomAdapter() {

    }

    public CustomAdapter(Context context, ArrayList<Integer> mWorkTW, ArrayList<Integer> mWorkET, ArrayList<Integer> mRestTW, ArrayList<Integer> mRestET, int numberOfIntervals) {

        this.mWorkTW = mWorkTW;
        this.mWorkET = mWorkET;
        this.mRestTW = mRestTW;
        this.mRestET = mRestET;
        this.mContext = context;
        this.numberOfIntervals = numberOfIntervals;
        //this.inputTimeIntegerWET = inputTimeIntegerWET;

        Log.d(TAG, "CustomAdapter: " + numberOfIntervals);
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View customView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.time_row, viewGroup, false);
        ViewHolder holder = new ViewHolder(customView, new InputTextListener());
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        Log.d(TAG, "onBindViewHolder: called");
        viewHolder.workTextView.setText(R.string.work_text_view);
        viewHolder.restTextView.setText(R.string.rest_text_view);
        viewHolder.workEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    viewHolder.workEditText.setHint("");
                else
                    viewHolder.workEditText.setHint(mWorkET.get(viewHolder.getAdapterPosition()));
            }
        });

        viewHolder.restEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    viewHolder.restEditText.setHint("");
                else
                    viewHolder.restEditText.setHint(mRestET.get(viewHolder.getAdapterPosition()));
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "" + numberOfIntervals);
        return numberOfIntervals;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public InputTextListener inputTextListener;
        TextView workTextView;
        EditText workEditText;
        TextView restTextView;
        EditText restEditText;
        ConstraintLayout parentLayout;




        public ViewHolder(@NonNull View itemView, InputTextListener inputTextListener) {
            super(itemView);
            workTextView = itemView.findViewById(R.id.workTextView);
            workEditText = itemView.findViewById(R.id.workEditText);
            restTextView = itemView.findViewById(R.id.restTextView);
            restEditText = itemView.findViewById(R.id.restEditText);
            parentLayout = itemView.findViewById(R.id.parentLayout);
            this.inputTextListener = inputTextListener;


            workEditText.addTextChangedListener(inputTextListener);
        }
    }

        class InputTextListener implements TextWatcher {
        String inputTimeString;

        int inputTime;
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();

            public HashMap<String, Integer> getHashMap() {
                return hashMap;
            }

            public InputTextListener() {
            }

            public void setHashMap(HashMap<String, Integer> hashMap) {
                this.hashMap = hashMap;
            }

            public int getInputTime() {
            return inputTime;
        }

        public void setInputTime(int inputTime) {
            this.inputTime= inputTime;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
             try {
                 Log.d(TAG, "onTextChanged: I've made it to here!");
                 inputTimeString = s.toString().trim();
                 inputTime = Integer.parseInt(inputTimeString);
                 setInputTime(inputTime);
                // hashMap.put("EDITTEXT VALUE", inputTime);
                 Log.d(TAG, "onTextChanged: " + inputTime);

                 int bla = inputTime + 2;
                 Log.d(TAG, "onTextChanged: " + bla);
                 Log.d(TAG, "onTextChanged: " + hashMap.containsKey("EDITTEXT VALUE"));
                 Log.d(TAG, "onTextChanged: " + hashMap.get("EDITTEXT VALUE"));
                 Log.d(TAG, "onTextChanged: "+ getInputTime());
                 //setHashMap(hashMap);

             } catch (NumberFormatException NFE) {
                 mWorkET = null;
             }



        }

        @Override
        public void afterTextChanged(Editable s) {

        }


    }
}
public class TimeActivity extends AppCompatActivity {

    public static final String TAG = TimeActivity.class.getSimpleName();
    private int numberOfIntervals;
    private ArrayList<Integer> WTV = new ArrayList<>();
    private ArrayList<Integer> WET = new ArrayList<>();
    private ArrayList<Integer> RTV = new ArrayList<>();
    private ArrayList<Integer> RET = new ArrayList<>();
    private int inputTime;




   // private String yusuf = "5";




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.custom_menu, menu);

        Drawable continueImageDrawable = menu.findItem(R.id.continueItem).getIcon();
        continueImageDrawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
        //Every non-transparent pixel will be turned into white.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.continueItem:
                CustomAdapter a = new CustomAdapter();
                CustomAdapter.InputTextListener i = a.new InputTextListener();
                //HashMap<String, Integer> hashMap = i.getHashMap();
                //inputTime = hashMap.get("EDITTEXT VALUE");
                inputTime = i.getInputTime();
               // Log.d(TAG, "onOptionsItemSelected: " + hashMap.get("EDITTEXT VALUE"));
                //Log.d(TAG, "onOptionsItemSelected: " + hashMap.containsKey("EDITTEXT VALUE"));
                Log.d(TAG, "onOptionsItemSelected: " + inputTime);

                retrieveInputTime(inputTime);
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time);
        Log.d(TAG, "onCreate: Started");
        View timeRowLayout = findViewById(R.id.parentLayout);








        Intent intent = getIntent();
        numberOfIntervals = intent.getIntExtra("Interval Count", numberOfIntervals);
        Log.d(TAG, "" + numberOfIntervals);

        initializeViews();
    }



    private void retrieveInputTime(int inputTime) {

        Log.d(TAG, "retrieveInputTime: " + inputTime);

       Intent intent2 = new Intent(this, ClockActivity.class);
        if (inputTime > 0) {
            intent2.putExtra("Input Time", inputTime);
            startActivity(intent2);
            Log.d(TAG, "retrieveInputTime: The data has been retrieved" + inputTime);
        } else {
            Toast.makeText(this, "Must enter a whole number 2", Toast.LENGTH_SHORT).show();
        }

    }

    private void initializeViews() {
        Log.d(TAG, "initializeViews: Preparing views");
        //Make sure they can change through the R.strings


        WTV.add(R.string.work_text_view);
        WET.add(R.string.default_time_value);
        RTV.add(R.string.rest_text_view);
        RET.add(R.string.default_time_value);

        initializeRecyclerView();
    }

    private void initializeRecyclerView() {
        Log.d(TAG, "initializeRecyclerView: Initialize RecyclerView");
        RecyclerView intervalRecyclerView = findViewById(R.id.intervalRecyclerView);
        CustomAdapter adapter = new CustomAdapter(this, WTV, WET, RTV, RET, numberOfIntervals);
        intervalRecyclerView.setAdapter(adapter);
        intervalRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
}

我希望getInputTime方法在TimeActivity类内返回正确的值

3 个答案:

答案 0 :(得分:0)

我看到您在两个地方使用了getInputTime():

  • 在onTextChanged()之前是setInputTime(),因此它应该在那里具有值
  • 在onOptionsItemSelected()内,它是通过InputTextListener()创建新对象后立即调用的。除了调用默认构造函数外,不执行任何初始化。此时inputTime应该为0。

答案 1 :(得分:0)

您在onOptionsItemSelected中引用的CustomAdapater实例与您要在onCreate中为recyclerView初始化的实例不同。

外观:

CustomAdapter a = new CustomAdapter();
CustomAdapter.InputTextListener i = a.new InputTextListener();
inputTime = i.getInputTime();

您正在创建CustomAdapter的新实例,然后创建InputTextListener的新实例(到目前为止这些对象之间的零连接),对该对象调用getInputTime()显然将返回0,因为它是java中int的默认值。

无论如何,我看不出在您的代码中使用recyclerView的意义。

答案 2 :(得分:0)

首先,我要感谢所有人为帮助解决问题而付出的努力,但我终于解决了。解决方案是使InputTextListener类内的inputTime变量静态。但是由于它是一个内部类,所以我必须将InputTextListener类设为自己的类。如果您有任何想法或疑问,请在下面留言。