I have 2 APIs. From API1 I'm fetching a list of category names and setting it on spinner. From API2 I'm getting a category name, which I want to set as selected item in the spinner. But the problem is that the first time I'm not getting any position. When I go back to previous activity and come back to my activity, this time I'm getting the position.
private int getIndex(Spinner spinner, String myString){
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).equals(myString)){
index = i;
}
}
return index;
}
// API2's JSON parsing
if(newobj.getString("cat_name")!=null){
int spinnerPosition = getIndex(catspin, newobj.getString("cat_name"));
catspin.setSelection(spinnerPosition);
}
Related
I have created 2 spinners say city and location. I have saved city and location data in local database and fetch cities from local database and show them.
Now, my problem is this I have added "select city" position on 0th position of array list. When I select any city and run command to get location based on city then I get wrong locations based on position.
I am not able to get correct position.
// code
dumy = new Dumy(this);
cityAreaModelsList = dumy.getOnlyCities();
cityList = new ArrayList<String>();
cityList.add(0, "Select City");
for (int i = 0; i < cityAreaModelsList.size(); i++) {
cityList.add(cityAreaModelsList.get(i).getCityName());
}
// spinner code
select_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Log.e(TAG, "Select City " );
} else {
cityId = (String) cityAreaModelsList.get(position).getCityId();
Log.e(TAG, "onItemSelected: "+cityId );
}
}
}
// say i have selected new delhi its city id is 1 but it gives me 2. Why? Please help
because of position of adapter starting from 1 (only data part)
and cityAreaModelsList is accessing from 0;
try below code:
cityId = (String) cityAreaModelsList.get(position-1).getCityId();
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
}
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);