Listview and dialog in android - 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);

Related

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

Problems changing the background colors in an android listview without clicking

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.

Problem in Android Spinner inside the Tabbars

I am making an application and i am facing a problem.
I am using Tab bars and in one of my Tab bar i am using Spinner.
It loads Perfectly but when i click on it. it gives me:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord#44647ef8 is not valid; is your activity running?
This error.
i am using the following code snippet
ArrayList<String> ageList;
Spinner age;
age = (Spinner) findViewById(R.id.country);
ageList = new ArrayList<String>();
ageList.add("10-20");
ageList.add("21-35");
ageList.add("36-60");
ageList.add("61-100");
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ageList); //array you are populating
adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
age.setAdapter(adapter2);
age.setSelection(0, true);
This code work fine when i am using seperate activity not the tab bars. but in tab bars it gives me the above error exception when i click on the spinner to open a list.
Please guide me Thanks a bunch
I m done with this
the problem was with my layout
setContentView(R.layout.age)
Instead of this i used Layout Inflator as below:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.country, null);
this.setContentView(viewToLoad);
and then called the Spinner n Bingooo! It works Just Perfect
Thanks all of you.. :)

Problems with creating spinner

In my app I'm developing, I have a spinner in an alert dialog box.
The spinner works fine, but when I added the following lines to add the array to the spinner, my app crashing a few seconds after starting up:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.newFileTypeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
newFileType.setAdapter(adapter);
What am I doing wrong?
These Android spinners seem to be a bit complicated as well, I don't think I'll be able to remember how to make them without referencing to the Android docs.
Problem solved.
I realised that the following line:
final Spinner newfiletypespinner = (Spinner) findViewById(R.id.newfiletypespinner);
I had to change to:
final Spinner newfiletypespinner = (Spinner) newFileDialogInflated.findViewById(R.id.newfiletypespinner);
With "newFileDialogInflated" being the previously inflated view so I could have a custom AlertDialog view:
final View newFileDialogInflated = View.inflate(this, R.layout.newfileview, null);
But thanks for the help!
Hard to say for sure whether you've omitted it from your snippet or omitted it from your code, but do you initialize newFileType as a spinner?
Spinner newFileType = (Spinner)findViewById(R.id.newFileTypeSpinner);
or similar? If you're trying to set its adapter before initializing it, that would explain it.

Categories

Resources