• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Can't make Toast

Greum

Well-Known Member
I'm following a tutorial to create and edit a simple database/table. All works fine, so I thought I would add a Toast message to let the user know the record had been added. Seemed simple enough, just add

Code:
Toast.makeText(getApplicationContext(), "Record added", Toast.LENGTH_SHORT).show();

after the database update. Thing is, it doesn't seem to understand getApplicationContext(). Every example I've seen for Toast is the same, so I'm stymied.

I don't know if the fact I'm using androidx makes any difference...
 
getApplicationContext() is a method defined by the Activity class. If the class that you're using this line of code in doesn't inherit from the Activity class, then the compiler will complain about it.
 
Ah, I'm adding it in a helper method in a DataManager class.

Edit trying to add it.
When I've run into that sort of issue, I pass the activity into the helper method, something like
Code:
public void myHelperMethod(Activity a) {
    // other code
    Toast.makeText(a.getApplicationContext(), "Record added", Toast.LENGTH_SHORT).show();
    // other code
}
 
Thanks, Tony. I see how to get the activity once passed, but I'm not sure how to pass it, and whether using fragments, it's going to work. This is what I've tried:

MainActivity (just for completeness)

Java:
InsertFragment fragment = new InsertFragment();
transaction.replace(R.id.fragmentHolder, fragment);

InsertFragment

Java:
btnInsert.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dm.insert(editName.getText().toString(),
            editAge.getText().toString(),
            this.MainActivity);
    }
});

DataManager

Java:
public void insert(String name, String age, Activity a) {
    String query = "INSERT INTO " + TABLE_N_AND_A + " (" +
            TABLE_ROW_NAME + ", " +
            TABLE_ROW_AGE +
            ") " +
            "VALUES (" +
            "'" + name + "'" + ", " +
            "'" + age + "'" +
            ");";
    Log.i("insert() = ", query);
    Toast.makeText(a.getApplicationContext(), "Record added", Toast.LENGTH_SHORT).show();
    mDB.execSQL(query);
}
 
Pass the MainActivity object in the Fragment constructor

Code:
InsertFragment fragment = new InsertFragment(this);
 
Just checking, you have added the new parameter to the declaration of the InsertFragment constructor, right?

Code:
public class InsertFragment {
  public InsertFragment(Context activity) {
    ...
  }
  ...
}
 
Yup. And remember to call the super constructor

Code:
public class InsertFragment {
  public InsertFragment(Context activity) {
    super();
    ...
  }
  ...
}
 
I realise I'm being a prize numpty, but this is what I have now and I know it's not right...
Java:
public class InsertFragment extends Fragment {
    public InsertFragment(Context activity) {
        super();
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_insert, container, false);

        final DataManager dm = new DataManager(getActivity());

        Button btnInsert = v.findViewById(R.id.btnInsert);
        final EditText editName = v.findViewById(R.id.editName);
        final EditText editAge = v.findViewById(R.id.editAge);

        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dm.insert(editName.getText().toString(),
                        editAge.getText().toString(),
                        this.activity);
            }
        });

        return v;
    }
}
 
Back
Top Bottom