I have created Spinner in my app and when I do something I'm changing my adapter. After the change when I open the spinner it should refresh the list of the names but it stays the same. When I check the adapter I can see that list is refreshed correctly but it doesn't shows in the spinner. Here is my code:
public void addItemsToSpinner(UserSettings settings) {
List<Recipient> list = settings.getRecipients();
Recipient selectedRecipient = settings.getSelectedRecipient();
List<String> nicks = new ArrayList<>();
if (selectedRecipient != null) {
Log.d("utolsó","selected name " + selectedRecipient.getNickname());
nicks.add(selectedRecipient.getNickname());
}
for (Recipient recipient: list) {
if (selectedRecipient == null || !recipient.getNickname().equals(selectedRecipient.getNickname())) {
nicks.add(recipient.getNickname());
}
}
for(int i = 0 ;i<nicks.size();i++){
Log.d("TAG","print the order = " + nicks.get(i));
}
dataAdapter = new ArrayAdapter<>(getActivity(), R.layout.spinner_item, nicks);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(dataAdapter);
dataAdapter.notifyDataSetChanged();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_recipient_chooser, container, false);
mSpinner = (Spinner) view.findViewById(R.id.spinnerChooser);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
dataAdapter.notifyDataSetChanged();
if (mListener != null) {
mListener.onRecipientSelected(position);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
When I the order it's correct but in the spinner it's not. Can anyone tell me why?
have you tried using adapter.notifyDataSetChanged(); after list is changed
Related
I'm very new to android programming, I'm trying to create 3 spinners, these spinners will be in the form of country (122 units) province (10000) and district (15000) and when the country is selected, the provinces of that country will appear on the spinner and the districts belonging to it will appear on the spinner. or how can I add the district if and else make this situation very complicated. How should I use a structure? Thank you for your help
{
spinnerIller = (Spinner) findViewById(R.id.spinneriller);
spinnerIlceler = (Spinner) findViewById(R.id.spinnerilceler);
dataAdapterForIller = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, iller);
dataAdapterForIlceler = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,ilceler0);
dataAdapterForIller.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterForIlceler.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerIller.setAdapter(dataAdapterForIller);
spinnerIlceler.setAdapter(dataAdapterForIlceler);
spinnerIller.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
((TextView) view).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gri));
if(parent.getSelectedItem().toString().equals(iller[0])) {
dataAdapterForIlceler = new ArrayAdapter<String>(Register.this, android.R.layout.simple_spinner_item, ilceler0);
}else if(parent.getSelectedItem().toString().equals(iller[1])) {
dataAdapterForIlceler = new ArrayAdapter<String>(Register.this, android.R.layout.simple_spinner_item, ilceler1);
}else if(parent.getSelectedItem().toString().equals(iller[2])) {
dataAdapterForIlceler = new ArrayAdapter<String>(Register.this, android.R.layout.simple_spinner_item, ilceler2);
}
dataAdapterForIlceler.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerIlceler.setAdapter(dataAdapterForIlceler);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerIlceler.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
(TextView) view).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gri));
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
I want when user click on "قرآن" my program invisible else things mean tarjuma and tafsir,
same like when he clicks on "مع ترجمہ" my program visible Quran and tarjuma and hide the tafsir.
I am using spinner it works fine but after refresh my recyclerview but I want without refreshing the recyclerview, mean is that when the user selects any Spinner value recyclerview change the view.
refresh mean going to another activity.
anybody who help me!!
here is my RecyclerView Adapter
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final Quran quran = quranic.get(position);
if (Quran.spinner.equals("قرآن")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.reference.setText(quran.getReferences());
holder.tarjuma.setVisibility(View.INVISIBLE);
holder.tafsir.setVisibility(View.INVISIBLE);
} else if (Quran.spinner.equals("مع ترجمہ")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.reference.setText(quran.getReferences());
holder.tafsir.setVisibility(View.INVISIBLE);
} else {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.tafsir.setText(Html.fromHtml(quran.getTafsir()));
holder.reference.setText(quran.getReferences());
}
}
my MainActivity
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Quran.java
public static String spinner = "";
You just need to call notifyDataSetChanged(); on your recycler view adapter each time you select an item from your spinner.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Even better if you keep a reference to your recycler view adapter, then replace:
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
by:
adapter.notifyDataSetChanged();
}
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
//add this line
yourAdapterObject.notifyDataSetChanged();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
I have a ListFragment's list that contains complex layouts as items, these items contain custom spinners (MaterialSpinner).
MaterialSpinner chooseSubjectSpinner = adapter.getItem(position).findViewById(R.id.myCustomSpinner);
chooseSubjectSpinner.setAdapter(spinnerAdapter);
I've tried getChildAt(position), adapter.getItem(postition), adapter.getView(position, null, parent), and list.get(position) with the added prefix of .findViewById(R.id.myview), but it doesn't work. (It either returns NullPointerException at runtime or doesn't do anything at all!).
EDIT:
if you guys want the whole fragment code:
private ArrayList<View> listViews = new ArrayList<>();
private ViewAdapter adapter;
private List<String> spinnerItems = new ArrayList<>();
private MaterialSpinnerAdapter<String> spinnerAdapter;
public FragmentUnitTest() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_unit_test, container, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState){
spinnerItems.add("Hey!");
spinnerItems.add("Hello!");
spinnerItems.add("Hi!");
spinnerAdapter = new MaterialSpinnerAdapter<>(getContext(), spinnerItems);
adapter = new ViewAdapter(listViews, view.getContext());
setListAdapter(adapter);
addView(R.layout.cardview_with_spinner);
MaterialSpinner spinner = adapter.getItem(0).findViewById(R.id.custom_spinner);
RLogger.e("FragmentUnitTest", spinner);
spinner.setAdapter(spinnerAdapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Clicked: " + position + " .", Toast.LENGTH_SHORT).show();
}
Create a Custom spinner like this:
public class PTCustomSpinner extends ArrayAdapter {
public PTCustomSpinner(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
int count = super.getCount();
return count>0 ? count-1 : count ;
}
Handle it like below // BELOW XML LAYOUT CONTAINS ONLY TEXTVIEW WITH ID "temp"
PTCustomSpinner spinnerArrayAdapter = new PTCustomSpinner(PTExceptionMainPage.this, R.layout.xml_with_only_textview);
spinnerArrayAdapter.addAll(team_array);
spinnerArrayAdapter.add("Pick Team");
spinnerTeam.setAdapter(spinnerArrayAdapter);
spinnerTeam.setSelection(spinnerArrayAdapter.getCount());
spinnerTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (spinnerTeam.getSelectedItem() != "Pick Team") {
teamIdSelected = apiResponseTeam.get(i).getTeamID();
}
else if( spinnerTeam.getSelectedItem().equals("Pick Team"))
{
View v = spinnerTeam.getSelectedView();
((TextView) v).setTextColor(getResources().getColor(R.color.ash));
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I have a Fragment where I dynamically create spinners. When I select item from spinner the onItemSelected doesn't trigger. Can I use AdapterView.OnItemSelectedListener interface in my approach? What am I doing wrong? Is there a best practices of handling item selection?
Here is the code:
public class ArticleAddParamsFragment extends Fragment implements AdapterView.OnItemSelectedListener {
private LinearLayout addPrLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_article_addparams, container, false);
addPrLayout = (LinearLayout) rootView.findViewById(R.id.addparams_layout);
setUpSpinners(getArguments().getLong("idArt"));
return rootView;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// doesn't go here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// doesn't go here
}
private void setUpSpinners(long idArt){
RealmAddParam addParForArt = realm.where(RealmAddParam.class)
.findAll()
.where()
.equalTo("idTaskart", idArt)
.findFirst();
for (RealmAddPrmMain prm: addParForArt.getAddParams()) {
addPrLayout.addView(createSpinner(prm));
}
}
public Spinner createSpinner(RealmAddPrmMain adPrm) {
Spinner spinner = new Spinner(getActivity());
spinner.setPrompt(adPrm.getNameAdd());
spinner.setFocusableInTouchMode(true);
spinner.setFocusable(true);
final ArrayList<SpinnerAdPrObj> spObjects = new ArrayList<>();
if (adPrm.getVvalue() != null) {
String[] values = adPrm.getVvalue().replace("[","").replace("]","").split(",");
if (values.length > 1){
for (Integer i = 0; i < values.length; i++){;
if (values[i].split(":::").length > 1) {
spObjects.add(new SpinnerAdPrObj(values[i].split(":::")[0], values[i].split(":::")[2], values[i].split(":::")[1]));
} else {
spObjects.add(new SpinnerAdPrObj(values[i].split(":::")[0]));
}
}
}
}
ArrayAdapter<SpinnerAdPrObj> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, spObjects);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
return spinner;
}
}
SOLUTION WAS:
just to add spinner.setOnItemSelectedListener(this);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
This is my code for retrieving the list of the songs on the device. I want to play the selected song. Couldn't figure out how to use the onItemClickListener to do it. How do I do that in another activity?
public class SongsFragment extends Fragment {
public SongsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_songs, container, false);
ListView audioView = (ListView)view.findViewById(R.id.songView);
ArrayList<String> audioList = new ArrayList<>();
String[] proj = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME};
final Cursor audioCursor = getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
audioList.add(audioCursor.getString(audioIndex));
} while (audioCursor.moveToNext());
}
}
audioCursor.close();
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, audioList);
audioView.setAdapter(adapter);
return view;
}
Use audioView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Object song = list.getItemAtPosition(position);
//TODO play song
}
});
Basically, when you click a song item from the list view, you will just get the value of the selected song and then you can then play it.