I have a String countries[].
now when i click a button then on the onClick event this abc array is filled.suppose it is filled with the name of 10 countries.These 10 countries should be visible as a list so that i can choose any 1 country among the list. but i am not able to show them as a list.
My programme crashes if i use the following code :
ListView list = (ListView)findViewById(R.id.ListView);
list.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, countries));
As countries is filled later on the click of a button.So initially the countries array is empty and as the onCreate() is executed first it crashes.
I found the examples on net where there is pre-defined array.so it works.My array is filled in a onClick event.how can i display List at that moment???
Make list your member variable.
Move the setAdapter call inside your onClick()....That'll do.
Related
I am making an Simple Android App to test the sorting capability and then use it on my main project that I will be making.
I want to know that suppose I display a list of items to the user with the help of custom objects and custom ArrayAdapter in the listview and there I want to place a sorting button that will sort the list during runtime according to the choice of the user (ex: alphabetically, according to ratings,etc.).
How to display the new list to the user after sorting.
Should I again use the setAdapter method or is there any other way?
When you click Sort Button , perfom sorting on the list , and in the next line write
yourAdapter.notifyDatasetChanged();
it will update your list with the updated(sorted) Data.
First of all, sort your source data base on the choice of the user (ex: alphabetically, according to ratings,etc.)
Ex:
List<String> mData; // your source data
// sort your data ascending order
Collections.sort(mData);
// use your adapter to update your ListView
mArrayAdapter.notifyDataSetChanged();
If you call notifyDatasetChanged() in your adapter your list will be redrawn in the right order in the screen. :) So after you order you list, call adapter.notifyDatasetChanged()
I've been searching for a way to create a favorites list. Something like the expandable list view but instead of having fixed groups it should allow the user to add groups he desires. I use a HashMap<String, List<String>> for storing the subgroups and their Group's name. I also use a text file to load stored groups and subgroups.
Is it possible to reuse a List<String> to enter values into the HashMap. My multiple attempts have failed so I was wondering if there is a possible work around. This is basically what I did for my last attempt:
myList.add("Carrot");
myList.add("Cabbage");
myHash.put("Group1", myList);
myList.clear();
myList.add("Orange");
myList.add("Apple);
myHash.put("Group2", myList);
When I view the results on the emulator it displays Orange and Apple for both groups. Any suggestions would be appreciated since I don't program in java often.
myList.add("Carrot"); myList.add("Cabbage");
myHash.put("Group1", myList);
//replace clear to new object.
myList = new ArrayList();
myList.add("Orange"); myList.add("Apple);
myHash.put("Group2", myList);
I have a listview with radio buttons now i need to add one value from a edittext after typing some thing in the edittext the typed text should appear in the listview as next row.? how it is posible.?
ListView with simpleArrayAdapter :
Follow these steps it may help:
1. Fetch the value from edit text after the user types using gettext()
2. Insert this value in an array.
3. create a list view.
4. create an arrayAdapter with the already created array as source.
5. Now set the adapter with listview using (array variable).setAdapter(adapter name);
For every time the user updates use notifyDataSetChanged() to refresh your listview with the new contents.
Pseudo code:
onClick(){
array.add(editText.gettext());
adapter.notifyDataSetChanged();
}
Click here for more.
In my application i have one spinner with data Cart,Trolley,Lorry,Truck etc..
in a button click i am saving spinner selected item and other items in the data base.
Now in another button click i want to display all saved data,so in that i want to display previously saved spinner item first instead of default one.
How can i achieve this,please anyone suggest me
Ex:in spinner 1,2,3,4 displayed now if i select 3 and saved in data base now this time i want to show spinner data as 3,4,1,2.
It's much simple by getting index of spinner from DB and set the currently selected index on spinner item,for example if the spinner position stored in DB
then set it as
spinner.setSelection(2);// Note : Position starts from 0,1,2,3 on array
You can change spinner content as below
String[] items = new String[] {"3","4", "1","2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spinner2.setAdapter(adapter);
So I understand you correctly, you want to reorder the spinner items based on the user's previous selection? You just have to update the Adapter that you assigned to the spinner in that case.
I guess you wired up a simple ArrayAdapter in this case, so a basic solution would be to modify the order of the strings contained in that adapter, after selection.
ArrayList listArray = new ArrayList();
listArray.add("one");
listArray.add("two");
listArray.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listArray.toArray(strArray););
spinner2.setAdapter(adapter);
here took items as ArrayList and when user click on any item , break that arraylist in two part start to that point and point to last. then take one temp arraylist and add second part then first part so in that one .
and again call
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listArray.toArray(strArray););
spinner2.setAdapter(adapter);
i need to display four names continues in listview
for example
aravind
bose
guru
moorthy
aravind
bose
guru
moorthy
the set of values to be displayed in listview for "n" number of times
if i have mension 10times.the four names should be display for 10 times in listview.help me
for this you need to pass "n" as a parameter while calling your custom adapter.
Since its your custom adapter create your arrayList in the adapter to be used for the list view using the parameters of ArrayList(data to be displayed) and the "n". Using these create another arrayList which is used to display the listView data..
the call would be somewhat like this:
listAdapter = new CustomListAdapter(this, R.layout.list_view,
countryStringList, n);