0

So I have two classes. I'm trying to send integer data from one class to the other with a get method (getInputTime). The variable I'm returning inside the get method has a value. But when I use the get method inside of the other class called TimeActivity it just returns 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));

    }
}

I expect for the getInputTime method to return the correct value inside the TimeActivity class

3 Answers3

0

I see you use getInputTime() in two places:

  • inside onTextChanged() it is preceded by setInputTime() so it should has value there
  • inside onOptionsItemSelected() it is called right after creating a new object via InputTextListener(). No initialization is done besides calling the default constructor. inputTime should be 0 at this point.
  • Ok, but how should I do for the value to be the same as inside onTextChanged. – Nobeel Gardenish Aug 13 '19 at 19:45
  • Man, you are giving us two files out of a program which contains tens of files or who knows how much more. If I do not see the whole program, I cannot give you the way to wire these two classes. But if I see it, I will have to spend so much time to find the answer that I'd want to get paid. – Miroslav Todorov Aug 13 '19 at 21:30
  • I completely understand where you're coming from but could you just explain what you mean by me doing more initialization besides calling the default constructor? – Nobeel Gardenish Aug 13 '19 at 21:57
  • Obviously the InputTextListener needs to be injected in the TimeActivity object, after it was initialized in some other way outside of it. Then you could read its fields. In the way you have written it, you initialize the object and immediately read a field from it. This is not how you should do it. – Miroslav Todorov Aug 14 '19 at 14:21
0

The CustomAdapater instance that you are referring to in onOptionsItemSelected isn't the same as the one you are initializing for your recyclerView in onCreate.

Look:

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

You are creating new instance of CustomAdapter then creating new instance of InputTextListener (zero connection between these objects so far), calling getInputTime() on that object will obviously return 0, since it's the default value of int in java.

Anyway, I can't really see the point for using recyclerView in your code.

San Mo
  • 288
  • 2
  • 11
  • Thanks for answering. The reason I'm creating the two instances is because InputTextListener is a class inside of CustomAdapter and the only way to access it as far as I know is to first create an instance of CustomAdapter and then create an instance for InputTextListener. Also the reason I'm using the recyclerView is for creating a list. – Nobeel Gardenish Aug 13 '19 at 19:36
  • Now if you do have any suggestions on how to get the integer data please let me know. – Nobeel Gardenish Aug 13 '19 at 19:42
  • I don't think that you understand the basic concepts of OOP. You are referring to 2 instances of a class and expecting to get the same result. Example: `Cat smileyCat = new Cat("Smiley"); Cat sadCat = new Cat("Donald"); println(sadCat.name)` Output: donald – San Mo Aug 13 '19 at 19:55
  • I do understand the basic concepts of OOP but the problem is that the only way I know of how to access a nested class from another class is the way I'm doing it. If you do know how to do it then I'd appreciate it if you could show. – Nobeel Gardenish Aug 13 '19 at 21:02
0

First of all I want to thank everyone for trying to help solve the problem but I finally solved. The solution is to make the inputTime variable inside the InputTextListener class static. But since it's an inner class I had to make the InputTextListener class it's own class. If you have any thoughts or questions just comment down below.