Fast switching between fragments crush my app, how can I avoid that? - android

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())

Related

Listview doesn't appear if not declared into onCreate

I'm trying to populate a listView, but if I initialize it after onCreate it doesn't appear. I tried to initialize it into the onCreate and use adapter.notifyDataSetChanged(); after I insert values into my String array used to populate the listView but I have no success.
Does someone know how to solve this problem? I checked other questions but I didn't find an answer to my problem.
Thanks in advance.
Maybe you need to call adapter.notifyDataSetChanged() inside the UI thread.
Also it's better to use an ArrayList<String> instead of a String[]
Keep references to both the ArrayList and ArrayAdapter inside your activity class.
private ArrayList<String> items;
private ArrayAdapter<String> adapter;
Initialize your ArrayList<String> and set adapter in your onCreate
items = new ArrayList<String>();
// add initial items
items.add("1st item");
// create adapter
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
// set the adapter
listView.setAdapter(adapter);
Now whenever you want to change the items, call the add(), remove(), etc methods of the ArrayList and subsequently call adapter.notifyDataSetChanged(). The change to the ArrayList can be done in any thread, but adapter.notifyDataSetChanged() should be called in UI thread.
For example in a button press you might wanna do
items.add("New item");
adapter.notifyDataSetChanged();
Also you might wanna check these:
this and
this
PS: Sorry for my bad posting skills. I'm new.

Why getApplicationContext and Activity context inflate different style of the spinner?

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.

Android ListView with Images inside of ListFragment

I have been trying to get a listview inside of SherlockListFragment for about a week (total of like 12 hours) and I can't figure it out. I'm out of options here. So, I have three tabs, which all need to have listviews, with images. I managed to get the listviews working, but I just cannot seem to find a way to add images. I am a HUGE beginner at this as well.
Here is one of the three tab classes that extend SherlockListFragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] commands = this.getResources().getStringArray(R.array.currentmobs_selections);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.main_list_item, commands);
setListAdapter(adapter);
}
Now I did make a custom array for the listview in attempt to get it working, but I didn't even know how to implement the custom array adapter into the code. If anyone knows any way to get a image/text listview inside of a ListFragment I'd be soooo happy. Thanks!!
You need to use a Custom adapter instead of an array adapter.
Good tutorial here: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Use if listview holds only one string
ListAdapter listAdapter =
new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,command);
setListAdapter(listAdapter);

notifyDataSetChanged with SQLite

I'm sorry to ask about what seems to be a simple problem but I've been looking for a solution for hours and now I can't stand it anymore.
My app is very simple. I've got "tasks" in a SQLite db, and I populate a listView with this db like this :
_dataAdapter = new TaskAdapter(_c, _taskDAO.getTasks());
_listTask.setAdapter(_dataAdapter);
which returns an arrayList of Task.
Problem is, when I click the dialog to remove a task, it removes it from the db but it's still on the screen, that's why I need to refresh the listView.
case DialogInterface.BUTTON_NEUTRAL:
_taskDAO.removeTask(1);
_dataAdapter.notifyDataSetChanged();
break;
But it does not work. I think it's due to the fact that I remove the task from the db but not from the adapter but I'm not quite sure.
Anyway, if someone's got a solution ...
If your adapter is an ArrayAdapter or BaseAdapter, the data source of your ListView is not the DB itself but the List you provide to the adapter.
You must update the datasource of your adapter (give him a new list) then call
_dataAdapter.notifyDataSetChanged();
Another solution would be to recreate the adapter :
_dataAdapter = null;
_dataAdapter = new TaskAdapter(_c, _taskDAO.getTasks());
_listTask.setAdapter(_dataAdapter);
Hope it helps
try,
(listName.getAdapter()).notifyDataSetChanged();

Listview and dialog in android

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);

Categories

Resources