简体   繁体   中英

How to read values from inflatable Edit Text and put them in an ArrayList

I frequently have troubles with EditTexts and ArrayLists because I often use them. I was trying to take the inputted values from an inflatable edittext and put them into an ArrayList, but apparently, the values I am receiving are always 0,0,0 . How can I fix this?

Here is the code that I thought would work:

public void addCourses() {
    LayoutInflater inflater = LayoutInflater.from(this);
    for (int i = 0; i < this.coursesList.size(); i++) {
        View v = inflater.inflate(R.layout.item_course, null, false);
        this.courses.addView(v);
        final int position = CalculateActivity.this.scoreList.size();
        final int position2 = CalculateActivity.this.totalList.size();
        CalculateActivity.this.scoreList.add(0);

        EditText Score = (EditText) v.findViewById(R.id.Score);
        EditText Total = (EditText) v.findViewById(R.id.Total);

        Score.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }


            public void afterTextChanged(Editable editable) {
                percentage = Integer.parseInt(editable.toString());
                int perc = (int) percentage;
                CalculateActivity.this.scoreList.set(position, perc);

            }
        });

        Total.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void afterTextChanged(Editable editable) {
                hundredth = Integer.parseInt(editable.toString());
                int hund = (int) percentage;
                CalculateActivity.this.scoreList.set(position2, hund);

            }
        });




    }
}

If you need any other code to help you figure this out, just put it in the comments.

EDIT::: The initialization of ScoreList and TotalList looks as such

ArrayList<Integer> gradesList = new ArrayList();
ArrayList<Integer> scoreList = new ArrayList();
ArrayList<Integer> totalList = new ArrayList();

Also, the code above is used here:

public void onClick(View view) {

        for (int x = 0; x < scoreList.size()&& x < totalList.size(); x++)

        {
            CalculateActivity.this.gradesList.add(scoreList.get(x) / totalList.get(x));

        }



        for (int i = 0; i <  CalculateActivity.this.gradesList.size(); i++) {
             int crdtHrs = ((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours();

                totalCrdtHrs += (int) crdtHrs;
                averageGrade1 += (((int) CalculateActivity.this.gradesList.get(i)) * (((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours()));
                averageGrade2 = averageGrade1/totalCrdtHrs;



        }

        int gpaCalc = averageGrade2;


        CalculateActivity.this.gpa.setText(String.format(""+gpaCalc));
    }
}

also the problem is that when I Log.d the ArrayList of scoreList or TotalList, the values return as 0,0,0 no matter what value I put in.

use getText(). It would be simpler.

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mButton = (Button)findViewById(R.id.button);
mEdit   = (EditText)findViewById(R.id.edittext);

mButton.setOnClickListener(
    new View.OnClickListener()
    {
        public void onClick(View view)
        {
            Log.v("EditText", mEdit.getText().toString());
        }
    });}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM