I am trying to make a unit converter by following a youtube video. The problem is that he's doing all that in his main activity while I'm doing all that in a fragment. The errors are in the java class of the fragment UnitConverterFragment.java
as
public class UnitConverterFragment extends Fragment {
TextView convertFromDropdownTextView, convertToDropdownTextView, conversionRateText;
EditText amountToConvert;
ArrayList<String> arrayList;
Dialog fromDialog;
DialogFragment toDialog;
Button convertButton;
String convertFromValue, convertToValue, conversionValue;
String[] unit = {}; //TODO
//end
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.fragment_unit_converter, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//start
convertFromDropdownTextView = view.findViewById(R.id.convert_from_dropdown_menu);
convertToDropdownTextView = view.findViewById(R.id.convert_to_dropdown_menu);
convertButton = view.findViewById(R.id.conversionButton);
conversionRateText = view.findViewById(R.id.conversionRateText);
amountToConvert = view.findViewById(R.id.amountToConvertValueEditText);
arrayList = new ArrayList<>();
for (String i : unit) {
arrayList.add(i);
}
convertFromDropdownTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fromDialog = new Dialog(UnitConverterFragment.this);
fromDialog.setContentView(R.layout.from_spinner);
fromDialog.getWindow().setLayout(650, 800);
fromDialog.show();
EditText editText = fromDialog.findViewById(R.id.edit_text);
ListView listView = fromDialog.findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UnitConverterFragment.this, android.R.layout.simple_list_item_1, arrayList);
}
});
//end
}
}
The MainActivity.java
is:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//start
//end
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start
// end
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
break;
case R.id.nav_unit_converter:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new UnitConverterFragment()).commit();
break;
case R.id.nav_contact:
Toast.makeText(this, "Contacting...", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_share:
Toast.makeText(this, "Sharing...", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
super.onBackPressed();
}
The error I get is in the OnClickListener
at the last of UnitConverterFragment.java
. It says it requires a context. I'm really new to this so probably making a big blunder. I was watching this youtube video.
Firstly, you are passing the current context this which is fragmentContext itself inside Dialog but you need to pass the parent context. You, can do something like the below:
....
...
convertFromDropDownTextView.setOnClickListener(...){
...
fromDialog = new Dialog(requirActivity());
....
//And your ArrayAdapter should be like below:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);
....
}