Search code examples
androidandroid-edittextgettext

getText().toString() adds some numbers before what is typed


I have instanced the variable:

  editname = (EditText) findViewById(R.id.editname);

Then on click of a button I want to put it in a database:

//CHECK FORM
if (editname.getText().toString().matches("")){
...toast...
return;
}
check_value=editname.getText().toString();
//DATABASE
if (myDB.insertData(editname.getText().toString()){//Insert data
Toast.makeText(..., R.string.data_inserted + check_value, Toast.LENGTH_LONG).show();
}

Once it is on the database I send it to a fragment after a click on a button:

Fragment newFragment = new PreviewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();


String get_id = Integer.toString(myDB.getId(check_value)); //Get  ID //java.lang.NullPointerException
Bundle bundle = new Bundle();
bundle.putString("id", get_id);
transaction.replace(R.id.fragment1, newFragment);
newFragment.setArguments(bundle);
transaction.commit();

On the implementation of the fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view;
    view = inflater.inflate(R.layout.fragment1, container, false);
    String id;

    try {
        id = getArguments().getString("id");
    }catch (Exception e){
        Toast.makeText(getActivity(), "not found", Toast.LENGTH_LONG).show();
        return view;
    }


    Cursor res = myDB.getValues(id);
    }

And in that line I see java.lang.NullPointerException

Once I added the check_value I could see that the text I got was not what I introduced in the box but this:

2131099688(Followed by the text I introduced)

Anyone knows the reason for this? I already removed the app and cleaned the project but I still get the same.


Solution

  • It is just the result of what you put in your toast.

    Toast.makeText(..., R.string.data_inserted + check_value, Toast.LENGTH_LONG).show();
    

    The number before check_value is the id of the string data_inserted.

    Try to getString(R.string.data_inserted) instead of the id.