How to make Robolectric select a particular spinner item - android

I am new to Robolectric and am at a roadblock. I have some custom event that fires on selection of a item in spinner and i want to test that using robolectric. I saw that ShadowSpinner class provides helper function like stateSpinner.clickFirstItemContainingText to click a particular item. I populate my spinner with proper values which i test printing out each item as
for (int i = 0; i < spinner_items.length; ++i) {
spinner_items[i] = (String) spinner.getAdapter().getItem(i);
}
System.out.println("Spinner Items "
+ Arrays.asList(spinner_items).toString());
However when i use clickFirstItemContainingText with one of the po
pulated values, i get a IllegalArgumentException. No item found containing test which is strange as above code shows that particular value do exists.
Any help would be greatly appreciated

I couldn't get Robolectric to work with spinners. The problem is that the views for the list-items in the spinner won't exist apart from the one selected. This can be seen as spinner.getChildCount() will always return 1. Just call spinner.setSelection().

Related

Cant able to detect which position is clicked in recycler view

I am creating a contact list app using SQL. So I want a OnItemClickListener to detect which item is being clicked and pass it to another activity.
My work is at:
https://github.com/divyank00/ContactList
The error always is NullPointerException.
The problem with your code is that you are putting the position as an Integer Extra in your MainAcitivity, but you are trying to fetch it as a String in contact_details.java which isn't possible. To solve this:
Go to your contact_details.java class, move to Line 59:
//String position=intent.getStringExtra("pos");
//int pos=Integer.parseInt(position);
Replace these lines with this:
int pos = intent.getIntExtra("pos", 0);

How to get certain field from the arrayList object on spinner selection

I just created a spinner and make this spinner read from the database. and display them in the spinner. I already did that. But I want to right down the Id of the selected item.
for example : when I select the first raw called (1, tyat abdullah, sergurey )
I need the Id (1) only to be written down without the full record.
I need the Id (1) only to be written down without the full record.
Then when you are doing labels.add, only put the data you want to see
You can also use doctorsList.get(position) within the selection listener, and set the other text view accordingly.
For example, doctorsList.get(position).getId() seems to get the information you are asking for
simply use this to get the selected item index
int index = spinner.getSelectedItemPosition(); //this will give you the seleected item index
is that what you want ?
leme Know :)

Get the respective value from the array according to selected item position

I have a spinner and I'm getting its selected item position by MyOnItemSelectedListener. By the time I'm using an array adapter to load items to the spinner. I have loaded the items and it works perfectly. But I have a small problem. When I doesn't select a value then it shows position 0 but it has got the value of 1st array value. As shown in the below image,
What I want to do is when I haven't select a value then it should get any value from array. And when I select 1st item then only it should get 1st array value.
Array is a created from json response so it is not possible to add a item to array manually.
I have used How to make an Android Spinner with initial text "Select One" (aaronvargas's answer) to add slect option to spinner as 1st selection.
How can I achieve this? Any help will be highly appreciated.
You can use the logic like :
int selectedValue = -1;
if(position<=0){
//Means Item not selected
}else{
selectedValue = array[position-1];
}
I think you can use something like I have shown.
if(position==0){
variable = 0;
}else{
//use ur logic here
}

How to save the state of an item in a listview android

How will i go about saving the state of a listview item during scrolling, so that the recycler does not use it when displaying the next row being. i basically have a listview with 4 textviews and one of the textview is displayed based on a condition, derived from a database. For clarity sake, i will call that dependant textview "A".
my problem is that, simplest case: if "A" is being displayed for only the first item or any other row in the list and its not displayed anywhere else, when scrolling, "A" is displayed on other rows that should not have it. i understand the concept of listview reusing rows, but i can't figure out how to save it on an item, so it doesn't get used in another row. Here is a simplified code with just 2 textview:
holder.viewItemName = (TextView)view.findViewById(R.id.nameId);
holder.viewdescriptionStatus = (TextView)view.findViewById(R.id.viewdescriptionId);
int namecolumn = c.getColumnIndex(DBAdapter.NAME);
String name = c.getString(namecolumn);
holder.viewItemName.setText(name);
int description = c.getColumnIndex(DBAdapter.description);
String descriptionstatus = c.getString(description);
/*problem am having here*/
if(description != null){
holder.viewdescriptionStatus.setText("description available");
holder.viewdescriptionStatus.setTextColor(Color.BLUE);
}
as you can see, am using a viewHolder, but i have come to realise that viewHolder doesn't hold the logic as well. i don't know how to save the state of the holder.viewdescriptionStatus based on that condition. Most of the examples i have seen are based on checkboxes. Please anyone with ideas?.. it will be really appreciated.
P.S : i am using bindView() and newView() since i am using an SQLdatabase and SimpleCursorAdapter. i have the same issue with clicking, but i want to solve the scrolling part first. Thank you for your time.
Try this
holder.viewdescriptionStatus.setText(description != null ? "description available" : "");

ListView getCheckedItemPositions call issue

I am having issues with the getCheckedItemPositions call for ListView. The first call works but subsequent calls returned the same results even if the previous checked items were unchecked.
eg.
first call: positions 0 & 1 are checked, result shows 0 & 1 are checked
2nd call: position 0 & 1 are unchecked, result still shows 0 & 1 are checked.
Is this a bug or getCheckedItemPositions doesn't work this way? Can someone clarify please? Thanks!
Use valueAt() instead of get(), then use keyAt() to find the right index to the checked itmes works for me.
SparseBooleanArray checkedItems = lview.getCheckedItemPositions();
if (checkedItems.size() > 0) {
for (int i=0; i<checkedItems.size(); i++) {
if (checkedItems.valueAt(i)) {
Log.d("checked item: " + lview.getItemAtPosition(checkedItems.keyAt(i)));
}
}
}
Recently I had the same problem and found the answer here:
How to use getCheckedItemPositions of ListView
To get the indices of the selected items of a multi-select ListView, you can use getCheckedItemPositions() to return a SparseBooleanArray.
The function however has a trap that is not documented, is that even
you select one item and then deselect, the item is still included in
the array, although the value of the item is set to false.
So as others have said, you need to iterate thru the returned SparseBooleanArray to find the TRUE values.
Sample code in the link.
I think that is meant to be called when you are closing the list, like when it's used in a dialog, so you can save the settings.

Categories

Resources