I am working on an application for Android. For this I am making an Activity in which you select your country and then a spot in that country. I have one spinner that contains a list of all available countries. Now, what I want it to do is get the country that has been selected, then filter a list of spots that I have for the items that start with the country that has been selected. Then it should put the spots for the selected country into a different spinner. Just for clarity, the list of countries is just a list of countries, and the list of spots looks like:
Country1 - Spot1
Country1 - Spot2
Country2 - Spot1
Country2 - Spot2
And so on.
This is what I thought the code should work like:
Get selected country from spinner 1.
Make a new ArrayList containing the spots.
Make a second empty ArrayList.
For each entry of the ArrayList containing the spots, check if it starts with the selected country.
If so, add it to the second ArrayList.
Once this is all done, make an ArrayAdapter with the second ArrayList.
Set this ArrayAdapter for spinner 2.
I tried to achieve this with the following code:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selectedCountry = parent.getItemAtPosition(pos).toString();
ArrayList<CharSequence> arraylist = new ArrayList<CharSequence>();
arraylist.addAll(R.array.spots_array);
ArrayList<CharSequence> arraylist2 = new ArrayList<CharSequence>();
for (i=0; i<arraylist.size(); i++) {
String delimiter = " - ";
if ((arraylist(i).split(delimiter)).equals(selectedCountry)) {
arraylist2.add(arraylist(i).string.substring(string.lastIndexOf('-') + 1));
}
}
ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this, arraylist2<CharSequence>, android.R.layout.simple_spinner_item);
arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(arrayAdapter2);
spinner2.setOnItemSelectedListener(this);
}
But it gives several errors:
At addAll() it says: "The method addAll(int, Collection) in the type ArrayList is not applicable for the arguments (int)"
At arraylist it says: "The method arraylist(int) is undefined for the type Configuration"
At string (inside substring) it says: "string cannot be resolved"
I am still relatively new to Android, and am having a lot of trouble getting this working. Can anybody please help me out?
There is a lot of little mistakes in your code :
To access an element in an arraylist use the get(position) method
When you add your "spot_array", you actually add the id of the resource, not the array itself (see here)
Here is your code updated, it should works or may need some tweaks
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selectedCountry = parent.getItemAtPosition(pos).toString();
List<CharSequence> arraylist = new ArrayList<CharSequence>();
arraylist.addAll(Arrays.asList(getResources().getTextArray(R.array.spots_array)));
List<CharSequence> arraylist2 = new ArrayList<CharSequence>();
String delimiter = " - ";
for (int i=0; i<arraylist.size(); i++) {
String country = arraylist.get(i).toString();
if (country.contains(selectedCountry)) {
arraylist2.add(country.substring(country.lastIndexOf('-') + 2));
}
}
ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this, android.R.id.text1, android.R.layout.simple_spinner_item);
arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(arrayAdapter2);
spinner2.setOnItemSelectedListener(this);
}
You have several errors in your code.
Firstly, the method addAll of the ArrayList must take as an argument a Collection. You are passing an Android array id R.array.spots_array; bear in mind that the Android ids are integers.
The usually method to fetch a string array from Android resources is (inside an activity):
String[] myArray = getResources().getStringArray(R.array.spots_array);
Second error: you should access the ArrayList elements by calling the method get(position) , not directly (arraylist(position)). Something like arraylist.get(position).
Third error:
arraylist2.add(arraylist(i).string.substring(string.lastIndexOf('-') + 1));
should simply be arraylist2.add(arraylist.get(i)); for adding one list element to another.
More on ArrayLists can be found here.
Related
I m using Arrays.binarysearch on a string array in an android app.
my array has 10 items, one of them being an UPPER CASE entry. Now, I can get index of all items from the array, except for the upper case one, which shows arrayIndexOutOfBound exception. For details: I m developing an android app with two activities. Activity A contains a list populated by an a string-array( called infections). When a user clicks an item on the list, he is taken to Activity B. The item clicked is sent to Activity B via putStringExtra method with the list's OnItemClickListener. In Activity B I am trying to get the received item's index on the same string-array (infections). All the other items work fine, except for one which is in UPPER case (the fifth item on the array... AIDS). Here are important snippets of my code:
strings.xml
<string-array name="infections">
<item>Acne vulgaris</item>
<item>Actinomycosis</item>
<item>Acute otitis media</item>
<item>African sleeping sickness</item>
<item>AIDS</item>
<item>Amebiasis</item>
<item>Anthrax</item>
</string-array>
ActivityA.java
String[] infections = getResources().getStringArray(R.array.all_inf);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String clicked = adapter.getItem(position);
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra(CLICKED_STRING,clicked);
startActivity(i);
}
});
ActivityB.java
String[] infections = getResources().getStringArray(R.array.all_inf);
String received = getIntent().getStringExtra(ActivityA.CLICKED_STRING);
int index = Arrays.binarySearch(infections,received);
When the item "AIDS" is clicked on the list, the app gives an arrayIndexOutOfBounds exception. when i replace "AIDS" with "Aids", the app works fine. Any help??
First of all, you need a sorted array for binarySearch().
Documentation:
public static int binarySearch (Object[] array, Object value)
Added in API level 1
Performs a binary search for value in the ascending sorted array
array. Searching in an unsorted array has an undefined result. It's
also undefined which element is found if there are multiple
occurrences of the same element.
Parameters
array the sorted array to search.
value the element to find.
Returns the non-negative index of the element, or a negative index
which is -index - 1 where the element would be inserted.
Without seeing your code, I assume you are trying to do something like this:
import java.util.Arrays;
public class BinarySearch
{
public static void main(String[] args)
{
String[] array = {"hello", "there", "YOU"};
Arrays.sort(array);
int index = Arrays.binarySearch(array, "you");
System.out.print(array[index]);
}
}
Which will give you this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -4
at BinarySearch.main(BinarySearch.java:18)
Basically, the element wasn't found, so it's returning a negative index as described in the documentation. Then if you try to access index -4 in your array, of course you get an index out of bounds exception.
So first of all, make sure that you don't access a negative index in your array.
Also, you could do something like this to check for the uppercase version if the lowercase version of a String doesn't exist in the array:
import java.util.Arrays;
public class BinarySearch
{
public static void main(String[] args)
{
String[] array = {"hello", "there", "YOU"};
Arrays.sort(array);
int index = Arrays.binarySearch(array, "you");
if (index < 0){
String str = "you";
index = Arrays.binarySearch(array, str.toUpperCase());
}
if (index >= 0){
System.out.print(array[index]);
}
}
}
Edited after comment:
It seems like this approach would be better for what you need, just go through the list using a for loop:
Keep your ActivityA code as it is in the question.
ActivityB:
String[] infections = getResources().getStringArray(R.array.all_inf);
String received = getIntent().getStringExtra(ActivityA.CLICKED_STRING);
int index = -1;
for (int i = 0; i < infections.length; i++){
if (received.equals(infections[i]){
index = i;
break;
}
}
if (index != -1){
//use index
}
Please someone help me how to show all list array in listview, i have "eventname" which values are: "Party", "Study", "Exam".
and inside my for loop i have this codes and it only outputs "Exam"..
lv = (ListView) findViewById(R.id.textView);
List<String> your_array_list = new ArrayList<String>();
your_array_list.add(eventname);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(CalendarActivity.this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
You are not adding data in your arraylist correctly..you are adding only one data in arraylist.
String[] array ={"EXAM","Study"};
for(String str: array){
your_array_list.add(str);
}
You code works properly, you are adding just one item:
your_array_list.add(eventname);
If you want to add the items "Party", "Study", "Exam", you should add them one by one:
your_array_list.add("Party");
your_array_list.add("Study");
your_array_list.add("Exam");
Or much better, declare them in a constant array and add them in a loop:
String[] ITEMS = new String[]{"Party", "Study", "Exam"};
...
for(String item : ITEMS) {
your_array_list.add(item);
}
Also, consider using an enum instead of a constant array. But it depends on what you are specifically programming, of course.
I have a listView and I want to print the arrrayList which contains the selected items.
I can show the choice that I choose every time. i.e. if I select a choice, I can print it in a toast (I mark it in my code as a comment), but I want to print the whole choices together.
Any help please?
Thanks..
If I understand correctly, you want to display the contents of your arrayList in a Toast.
Like donfuxx said, you need to create your arrayList outside of your onclicklistener.
As the user clicks an item, it will be added to your arrayList.
Then loop over the list to fill a string called allItems, then show allItems in a toast.
ArrayList<String> checked = new ArrayList<String>();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String listItem = (String) listView.getItemAtPosition(position);
if(!checked.contains(listItem)){ //optional: avoids duplicate Strings in your list
checked.add((position+1), listItem);
}
String allItems = ""; //used to display in the toast
for(String str : checked){
allItems = allItems + "\n" + str; //adds a new line between items
}
Toast.makeText(getApplicationContext(),allItems, Toast.LENGTH_LONG).show();
}
});
Well you have the right concept, jsut wrong execution here is the part you missed out on:`
ArrayList<String> checked = new ArrayList<String>();
checked.add((position+1), listItem);
Toast.makeText(getApplicationContext(),checked.get((position+1)), Toast.LENGTH_LONG).show();`
You have to get the position of the element in the ArrayList which you require to fetch, hence
checked.get(array position of element here)
If you want to show every item that is in the ArrayList you can use a simple loop and add them to a string like this:
...
checked.add((position+1), listItem);
String tempString = "";
for(int x = 0; x < checked.length(); x++) {
tempString = tempString + ", " + checked.get(x);
}
tempString = tempString.substring(2);
Toast.makeText(getApplicationContext(),tempString, Toast.LENGTH_LONG).show();
EDIT modified it a bit to only put commas between items
I have:
a String array with an unknown length that's populated with unknown items (let's say fish, bird, cat)
an ArrayAdapter and a Spinner that displays the items
a variable that contains one unknown item from the string array (let's say cat)
I want to set the Spinner to the value from the variable (cat). What's the most elegant solution? I thought about running the string through a loop and comparing the items with the variable (until I hit cat in this example), then use that iteration's # to set the selection of the Spinner, but that seems very convoluted.
Or should I just ditch the Spinner? I looked around and found a solution that uses a button and dialog field: https://stackoverflow.com/a/5790662/1928813
//EDIT: My current code. I want to use "cow" without having to go through the loop, if possible!
final Spinner bSpinner = (Spinner) findViewById(R.id.spinner1);
String[] animals = new String[] { "cat", "bird", "cow", "dog" };
String animal = "cow";
int spinnerpos;
final ArrayAdapter<String> animaladapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, animals);
animaladapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bSpinner.setAdapter(animaladapter);
for (Integer j = 0; j < animals.length; j++) {
if (animals[j].equals(animal)) {
spinnerpos = j;
bSpinner.setSelection(spinnerpos);
} else {
};
}
(Temporarily) convert your String array to a List so you can use indexOf.
int position = Arrays.asList(array).indexOf(randomVariable);
spinner.setSelection(position);
EDIT:
I understand your problem now. If your String array contains all unique values, you can put them in a HashMap for O(1) retrieval:
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < animals.length; i++) {
map.put(animals[i], i);
}
String randomAnimal = "cow";
Integer position = map.get(randomAnimal);
if (position != null) bSpinner.setSelection(position);
I would like to set the position of a spinner item, but the spinner is being populated with a simplecursoradapter with data from a database. I would like to get the current string from the database, then set the spinner to that value. I know that by using an arrayadapter I can get the position from the adapter this way:
String myString = queryData.getString(queryData.getColumnIndex("myString"));
//cast to an ArrayAdapter
mySpinnerAdapter adapter = (mySpinnerAdapter) mySpinner.getAdapter();
int spinnerPosition = ?
//set the default according to value
mySpinner.setSelection(spinnerPosition);
But is there a way to get the position of an item from a SimpleCursorAdapter? In the past I have always build an array of my spinner data along side the cursoradapter, but this seems like a dirty way of doing it. If its the only way though...
You can get the position of the item by doing this:
ArrayList<View> views = new ArrayList<View>();
listView1.reclaimViews(views);
for (int i = 0; i < views.size(); i++)
{
View v = views.get(i);
// Get the view's text value and compare it with your string here
// If the two strings match, store the position, which is 'i' in this case
// If your view is a textview, you would do this:
TextView tv = (TextView)v.findViewById(R.id.textView1);
if (tv.getText().toString().toLowerCase().compareTo(textToCompare.toLowerCase()) == 0)
{
// Store position here
}
}
Even i face the same problem. After breaking my head for some hours, i found this solution.
I hope it will help you.
/// value is column name in DB. take value from
final String[] from = new String[] {"value"};
/// field in spinner. Put value into (keep "R.id.text1" as it is..)
final int[] to = new int[]{R.id.text1};
// Get Database Access Object to interact with DB
DataAccessObject dataAccessObject = new DataAccessObject(context);
final Cursor valueCursor = dataAccessObject.querySpinnerValues();
final SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
context, R.layout.spinner_row_layout, valueCursor,
from , to );
// when ever a field is selected in the spinner's "spinner.setOnItemSelectedListener" method will be called
// and the primary key
// associted with that particular value will be saved into "someFieldPkMap" map or you can choose other collection..or some
// instance variable..
//
// Note : Here i am fetching two columns fron DB.
// 1. "_id" Primary key should always be "_id" otherwise it will not work
// 2. "value" which will be displayed in the spinner. this column can be anything. but you need to map it. The way i did eg..
// final String[] from = new String[] {"value"}; /// value is column name in DB. take value from
// final int[] to = new int[]{R.id.text1}; /// field in spinner. Put value into (keep "R.id.text1" as it is..)
//
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
valueCursor.moveToPosition(position);
// Get the primary Key associated with the value...
// Use "someFieldPkMap" map to get the primary key
someFieldPkMap.put(fieldName, Integer.parseInt(valueCursor.getString(0)));
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
spinner.setAdapter(simpleCursorAdapter);