0

I want my app to display the user input of editText in a listView. I have tried by using setText method but the problem is the required args are string and what I am passing is editText. So what could I do to pass the editText.

MY mainActivity class package com.example.manuj.autocalc;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ListView;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

        EditText editTotal;
        EditText editItem;
        EditText editAmt;
        ListView listView;
        CustomAdapter customAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            editItem=findViewById(R.id.itemEdit);
            editTotal=findViewById(R.id.editTotal);
            editAmt=findViewById(R.id.amtEdit);

            final ArrayList<Calc> calcs=new ArrayList<>();
            listView=findViewById(R.id.listView);
            customAdapter=new CustomAdapter(this,-1,calcs);
            listView.setAdapter(customAdapter);
            Calc calc=new Calc(editItem.getText().toString(),editAmt.getText().toString(),editTotal.getText().toString());
            calcs.add(calc);
            customAdapter.notifyDataSetChanged();


        }
    }

This is my Adapter class

    package com.example.manuj.autocalc;

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;

    import java.util.ArrayList;


    public class CustomAdapter extends ArrayAdapter<Calc> {
        public CustomAdapter(Context context, int resource, ArrayList<Calc> calcs) {
            super(context, resource, calcs);
        }
        @Override
        public View getView(int position, View view, ViewGroup parent) {

            Calc calc = getItem(position);

            if (view == null){
                view = LayoutInflater.from(getContext()).inflate(
                        R.layout.list_item, parent, false);
            }

            //get and set the items
            EditText item=(EditText)view.findViewById(R.id.itemEdit);
            item.setText(calc.getmItemEdit());

            //get and set the amt
            EditText amt=view.findViewById(R.id.amtEdit);
            amt.setText(calc.getmAmtEdit());

            //get and set the totalAmt
            EditText total=view.findViewById(R.id.editTotal);
            total.setText(calc.getmTotalEdt());

            return view;
        }
    }


    **This is my model class**
    package com.example.manuj.autocalc;


    import android.widget.EditText;

    public class Calc {
        private String mItemEdit;
        private String mTotalEdt;
        private String mAmtEdit;

        Calc(String itemEdit,String totalEdit,String amtEdit){
            this.mItemEdit=itemEdit;
            this.mTotalEdt=totalEdit;
            this.mAmtEdit=amtEdit;

        }


        public String getmItemEdit(){return mItemEdit;}

        public String getmTotalEdt(){return mTotalEdt;}

        public String getmAmtEdit(){return mAmtEdit;}
    }
7
  • Change your code up here, edit question, because this is a new problem, your old code doesn't give that error Commented Aug 12, 2018 at 6:19
  • Ok I found the problem, it is because you put it in oncreate, where both your edittext got nothing, so when it run calc, it give you the exception Commented Aug 12, 2018 at 6:27
  • One way to solve is to create a button, when user click that button then only you run Calc calc=new Calc(editItem.getText().toString(),editAmt.getText().toString(),editTotal.getText().toString()); calcs.add(calc); customAdapter.notifyDataSetChanged(); Commented Aug 12, 2018 at 6:31
  • Okay i'll try.Thank you for your response. Commented Aug 12, 2018 at 6:35
  • No problem, I was once in your position, seeking for help, but no one willing to help, so I try to help people when I can Commented Aug 12, 2018 at 6:36

3 Answers 3

2

Your Calc class constructor needs 3 strings:

Calc calc=new Calc(editItem.getText().toString(),editTotal.getText().toString(),editAmt.getText().toString());
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your response,But after updating the code still its causing a error,runtime error,the app is terminated and the reason is null object refenece at the editText.widget at line no 32 in MainActivity.java.which is the answer you updated.Calc calc=new Calc(....).Pleasee help me how to solve thiis.
I had a different order of total and amt in my answer, try if this was causing the problem.
Which is line no 32?
Calc calc=new Calc(editItem.getText().toString(),editTotal.getText().toString(),editAmt.getText().toString()); This is line no 32 in mainActivity.java and it causes a null order reference
Then check the 3 findviewbyid if they return null, maybe you misspelled their ids
0

Use this code in the adapter class

 package com.example.manuj.autocalc;

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;

    import java.util.ArrayList;


    public class CustomAdapter extends ArrayAdapter<Calc> {
        public CustomAdapter(Context context, int resource, ArrayList<Calc> calcs) {
            super(context, resource, calcs);
        }
        @Override
        public View getView(int position, View view, ViewGroup parent) {

            Calc calc = getItem(position);

            if (view == null){
                view = LayoutInflater.from(getContext()).inflate(
                        R.layout.list_item, parent, false);
            }

            //get and set the items
            EditText item=(EditText)view.findViewById(R.id.itemEdit);
            item.setText(calc.getmItemEdit().getText().toString());

            //get and set the amt
            EditText amt=view.findViewById(R.id.amtEdit);
            amt.setText(calc.getmAmtEdit().getText().toString());

            //get and set the totalAmt
            EditText total=view.findViewById(R.id.editTotal);
            total.setText(calc.getmTotalEdt().getText().toString());

            return view;
        }
    }


    **This is my model class**
    package com.example.manuj.autocalc;


    import android.widget.EditText;

    public class Calc {
        private String mItemEdit;
        private String mTotalEdt;
        private String mAmtEdit;

        Calc(String itemEdit,String totalEdit,String amtEdit){
            this.mItemEdit=itemEdit;
            this.mTotalEdt=totalEdit;
            this.mAmtEdit=amtEdit;

        }


        public String getmItemEdit(){return mItemEdit;}

        public String getmTotalEdt(){return mTotalEdt;}

        public String getmAmtEdit(){return mAmtEdit;}
    }

Comments

0

You can pass this

(your edittext).getText().toString()

as your argument to the setText intended.

You can also try to use textchangedlistener like this:

Sample code change yourself:

EditText searchTo = (EditText)findViewById(R.id.medittext);
searchTo.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        doSomething();
    } 
});

Put your calc into onTextChanged but put at the last one of your edittext which take the user input, such as your app takes 2 input from user, which maybe edittext1 and edittext2, put the textchangedlistener at edittext2, else it will return null exception again

Hope this helps!

8 Comments

Android studio is not allowing me to add the getText() method.What to do?
@Manuj Sharma what do you mean? Any code or screenshot of it?
But it's causing a null pointer exception due to getText().what to do?
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.manuj.autocalc.MainActivity.onCreate(MainActivity.java:32)
yes , Calc calc=new Calc(editItem.getText().toString(),editAmt.getText().toString(),editTotal.getText().toString());
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.