I have a strange problem. I have 2 pre-defined spinner in the xml. However, the item and related dropdown item are rendering in runtime.
However, the following 2 statement provide different result of the layout. I have no idea why the result is like this. But the main difference is getApplicationContext() and this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getSrvNumList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, getSrvNumList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
As #MikeM. commented, it is the issue about the context. Hope the following answer can help someone with this issue.
Use Activity context instead of Application context
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this,
android.R.layout.simple_spinner_item, getSrvNumList());
Otherwise, the application will get different theme to render the spinner.
According to this post (look for Chapter 3), we can consider 2 types of Context: UI-Context (for example getActivity(), view.getContext()...) and non-UI-Context (such as getApplicationContext()...).
When inflate view, let use UI-Context to keep its application theme. That rule is:
* Do you need to access UI related stuff? Use UI-Context.
* Otherwise, Use Non-UI Context.
Related
I am trying to understand what other alternative I have if I can't write this code:
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,bluetoothDevices);
recyclerView.setAdapter(arrayAdapter);
I have started to learn Java and I am taking a freshman class that requires us to write a mobile app, but there is no instruction on how to do this. This class is not a programming class, it's a project based course so everyone has to learn everything on their own. I have literally been introduced to android a week ago.
My other question is how does android store text files?
ArrayAdapter isn't meant to be used with RecyclerView. If you want to keep your ArrayAdapter, you can change your RecyclerView to a ListView:
// Change to a ListView
// (Remember to change it in your layout as well)
ListView myListView = findViewById(R.id.my_list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myStringItems);
myListView.setAdapter(adapter);
This will work fine for simple data but, if you need to display more complex data or if you require more customization or performance, using RecyclerView is the recommended approach.
I'm newbie in programming.
I'am adding entries to spinner with data from firebase, everything is ok, but when im fast switching between fragments, error occurs.
I know the problem is that im using "getActivity" while using fragment, and the code is still excecuting, while the fragment is not attached anymore. And thats the problem.
Is there any posibility to attach adapter to spinner from an activity instead of from fragment?
Imo that could avoid that problem, but i dont know how to do that?
Here is some code.
ArrayAdapter dania = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menulist); //I KNOW THAT THE PROBLEM IS HERE getActivity()
ArrayAdapter adodatki = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menudodatkilist); //I KNOW THAT THE PROBLEM IS HERE getActivity()
dania.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adodatki.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
danie1spinner.setAdapter(dania);
danie2spinner.setAdapter(adodatki);
I'm open for all suggestions.
Thanks for all answers :)
Thanks #SteveM,
For me it worked to use
if (getActivity() != null)
{
ArrayAdapter dania = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menulist); //I KNOW THAT THE PROBLEM IS HERE getActivity()
ArrayAdapter adodatki = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menudodatkilist); //I KNOW THAT THE PROBLEM IS HERE getActivity()
dania.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adodatki.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
danie1spinner.setAdapter(dania);
danie2spinner.setAdapter(adodatki);
}
thanks one more time for an easy idea :D
ArrayAdapter requires a Context as the first parameter. Simply pass the Context of the Fragment instead. (I think it's GetView())
When my application loads, I need some(not necessarily all) of the items in my list view to change their backgrounds without clicking on the ones I need. I know the method to change background, but I don't know how to extract the views from the ListView to do that.
I looked at this question and the answer did not work:
https://stackoverflow.com/questions/10650692/how-to-change-background-color-list-view-item-without-click-on-any-of-them
Here is some of my code:
mApprovalList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.approvals_array)));
mAdapter = new ArrayAdapter<String>(this,
R.layout.approvals_list,
immApprovalList);
setListAdapter(mAdapter);
ListView lv = getListView();
Log.v(String.valueOf(lv.getChildCount()), "count");
Also, that "log" returns 0.
Thanks in advance!
You should write a custom adapter and have it set the backgrounds to whatever color you'd like. See https://stackoverflow.com/a/5561813/458968
Then use
MyCustomAdapter customAdapter = new MyCustomAdapter(this, mApprovalList);
setListAdapter(customAdapter);
in your onCreate method.
I have a small custom dialog in wich I have a ListView with id:list. I want to populate it with strings I have in my resources (R.array.tones), but I have really big problems making this work, have tried many different solutions this is the latest which I think would work but it throws an null pointer exception on the toneList.
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.tone_dialog);
dialog.setTitle(R.string.tonePromptTitle);
ListView toneList = (ListView)findViewById(R.id.list);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tones, android.R.layout.simple_list_item_1);
toneList.setAdapter(adapter);
dialog.show();
My class is just extending Activity not ListActivity, and I would like to have it that way otherwise I must create a new class just for the listview.
I apologize for the long code, I'm new here and hasn't really figured out all functionality yet.
You should look for the View inside of the dialog:
ListView toneList = (ListView)dialog.findViewById(R.id.list);
:)
You are not searching your dialog layout for toneList. Try the following instead:
ListView toneList = (ListView)dialog.findViewById(R.id.list);
I am trying to create a list of strings from my arrays.xml file, as found in the android docs too. This is what they use:
ArrayAdapter adapter = ArrayAdapter.createFromResource(context, R.array.colors, android.R.layout.simple_spinner_item);
eclipse is warning that the ArrayAdapter instance is a raw type. Should it really be:
ArrayAdapter<CharSequence> adapter = ...;
?
Thanks
HiIt's always better to leave type checking to java compiler, so it would be better to declare your adapter as
ArrayAdapter<CharSequence> It probably doesn't change anything in your code, especially when you are sure that you are not going to put anything else but strings to your adapter. But it's definately a good practice to use generics to avoid runtime exceptions.Regards!