Reputation: 11
I'm working with Fragments
in my current app. I tried using a Spinner in one of the fragments, to sort the listview
below it in a specific format. The Adapter
is set correctly, but I'm unable to open the dropdown on touch to select the choices. How I know the adapter is set correctly, is that in the AVD
, I'm able to navigate to it using the keyboard and open it.
Code:
public class Upcoming_Reminders extends Fragment {
private View view;
private ProgressDialog progressDialog;
private ListView listView;
private String TAG = "Loggin";
RemindersAdapter remindersAdapter;
private Spinner mSortSpinner;
private String[] test;
public Upcoming_Reminders() {
// Required empty public constructor
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mSortSpinner = (Spinner) view.findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> mSortAdapter = new ArrayAdapter<CharSequence>(getContext(), android.R.layout.simple_spinner_item, test);
mSortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSortSpinner.setAdapter(mSortAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_upcoming__reminders, container, false);
test = getActivity().getResources().getStringArray(R.array.sort_by);
listView = (ListView) view.findViewById(R.id.contracts_list);
return view;
}
I tried putting it in the OnCreateView
method as well, below the view assignment. It still isn't clickable.
The XML
<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="com.onerooftechnologiesamc.Upcoming_Reminders">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="70dp">
<TextView
android:id="@+id/te"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|end"
android:text="@string/sort_by" />
<Spinner
android:id="@+id/sort_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
/>
</LinearLayout></FrameLayout>
What could be the cause? I tried Googling a lot, haven't found someone with the exact same problem as me.
Thank you
Edit 1: The Resources file
<resources>
<array name="sort_by">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
<item>Option 4</item>
</array></resources>
Upvotes: 0
Views: 2881
Reputation: 11
The problem seems to have been with FrameLayout. I switched to a RelativeLayout and it started working perfectly.
Upvotes: 0
Reputation: 36
public class SpinnerFragment extends Fragment {
private Spinner spinner;
private String[] test = {"1","2","3","4","5"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spinner,container,false);
spinner = (Spinner) view.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> mSortAdapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item, test);
mSortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mSortAdapter);
return view;
}
}
Upvotes: 1
Reputation: 36
Try this:
public class SpinnerAdapter extends ArrayAdapter<String>
{
private Activity context;
String[] data = null;
public SpinnerAdapter(Activity context, int resource,
String[] data2)
{
super(context, resource, data2);
this.context = context;
this.data = data2;
}
...
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent)
{
View row = convertView;
if(row == null)
{
//inflate your customlayout for the textview
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.spinner_layout, parent, false);
}
//put the data in it
String item = data[position];
if(item != null)
{
TextView text1 = (TextView) row.findViewById(R.id.rowText);
text1.setTextColor(Color.WHITE);
text1.setText(item);
}
return row;
}
...
}
and then set the adapter for the spinner:
Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
final String[] data = getResources().getStringArray(
R.array.data);
final ArrayAdapter<String> adapter = new SpinnerAdapter(
MainActivity.this, android.R.layout.simple_spinner_item,
data);
mySpinner.setAdapter(adapter);
Upvotes: 0