I'm developing a small app that needs multiple tabs, each with different data inputs, all was going ok using fragments, one of these inputs must be made in the form of a spinner, but doing so results in "Cannot solve symbol" on 'parent' and 'position', also 'createFromResource (android.content.Context, int, int)' in 'android.widget.ArrayAdapter' cannot be applied to '(com.example.sinalt2.fragment2, int, int)', any way the code is here, any help is appreciated.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.documento, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String text = parent.getItemPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@ id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="43dp"
android:layout_marginEnd="8dp"
android:text="Nome *" />
<EditText
android:id="@ id/editTextTextPersonName4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Digite seu nome"
android:inputType="textPersonName"
android:minHeight="48dp" />
<TextView
android:id="@ id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="8dp"
android:text="Tipo de documento *" />
<Spinner
android:id="@ id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="260dp"
android:layout_marginTop="78dp"
android:layout_marginEnd="8dp"
android:entries="@array/documento"
android:minHeight="48dp" />
<TextView
android:id="@ id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="66dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="8dp"
android:text="Nº Documento *" />
<EditText
android:id="@ id/editTextNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="125dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="number"
android:minHeight="48dp" />
<TextView
android:id="@ id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="102dp"
android:layout_marginTop="190dp"
android:layout_marginEnd="8dp"
android:text="Telefone *" />
<EditText
android:id="@ id/editTextPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="176dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="@ id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="114dp"
android:layout_marginTop="245dp"
android:layout_marginEnd="8dp"
android:text="E-mail *" />
<EditText
android:id="@ id/editTextTextEmailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="226dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="textEmailAddress"
android:minHeight="48dp" />
<TextView
android:id="@ id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="70dp"
android:layout_marginTop="290dp"
android:layout_marginEnd="8dp"
android:text="Município/UF *" />
</FrameLayout>
CodePudding user response:
You need to adapt the code inside the onCreateView
as follows as the return type of onCreateView
is View. Am also assuming that the spinner is inside the fragment_fragment2
layout.
Also for ArrayAdapter.createFromResource
you need to pass context. Since you are in a fragment this
refers to Fragment
. Hence you need to pass getAcitvity()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment2, container, false);
Spinner spinner = view.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),R.array.documento,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
return view;
}
Update:
You also need to adapt the code inside the onItemSelected
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
String text = parent.getItemAtPosition(i).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}