Reputation: 53
I have created a Fragment called PaletteFragment that includes a spinner. However, when I click on the spinner, the dropdown items do not appear. It is allowing me to click on the spinner, but doesn't do anything. The spinner did work when the assignment didn't require me to use fragments (as shown below) but now that we are required to use fragments, the spinner does not work even though I'm getting no errors in the logcat.
This is what I am trying to get again from the fragment
This is what I am getting after moving my PaletteActivity to my fragment
Here is my PaletteFragment
package edu.temple.coloractivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import java.util.Locale;
/**
* A simple {@link Fragment} subclass.
* Use the {@link PaletteFragment #newInstance} factory method to
* create an instance of this fragment.
*/
public class PaletteFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public static PaletteFragment newIntance(){
return new PaletteFragment();
}
public PaletteFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment PaletteFragment.
*/
// TODO: Rename and change types and number of parameters
public static PaletteFragment newInstance(String param1, String param2) {
PaletteFragment fragment = new PaletteFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_palette, container, false);
Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.myColors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<CharSequence> stringNames = ArrayAdapter.createFromResource(getActivity(), R.array.myStrings, android.R.layout.simple_spinner_item);
stringNames.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<CharSequence> stringName = ArrayAdapter.createFromResource(getActivity(),R.array.myStrings, android.R.layout.simple_spinner_item);
stringName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
View v = inflater.inflate(R.layout.fragment_palette,container,false);
spinner = v.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
}
else if(position == 1){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",1);
startActivity(i);
}else if(position == 2){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",2);
startActivity(i);
}else if(position == 3){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",3);
startActivity(i);
}else if(position == 4){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",4);
startActivity(i);
}else if(position == 5){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",5);
startActivity(i);
}else if(position == 6){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",6);
startActivity(i);
}else if(position == 7){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",7);
startActivity(i);
}else if(position == 8){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",8);
startActivity(i);
}else if(position == 9){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",9);
startActivity(i);
}else if(position == 10){
Intent i = new Intent(getActivity(), CanvasActivity.class);
i.putExtra("position",10);
startActivity(i);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
}
Here is my PaletteActivity
package edu.temple.coloractivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import java.util.Locale;
public class PaletteActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PaletteFragment PaletteFragment = new PaletteFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.firstLayout,PaletteFragment,PaletteFragment.getTag())
.commit();
}
}
This is the relevant code hopefully it is enough to spot what I am doing wrong, if not I'll post the whole code
Upvotes: 0
Views: 288
Reputation: 773
You doing everything ok, except your are not binding adapter with spinner. Use this to set Adapter for spinner.
stringName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
View v = inflater.inflate(R.layout.fragment_palette,container,false);
spinner = v.findViewById(R.id.spinner);
spinner.setAdapter(stringName )
Upvotes: 1