How to retrieve position of an element from a custom adapter based on element name.. I have a cityAdapter that contains city name and id in key value format. I am trying to retrieve position by the following code
SpinnerModel model=new SpinnerModel();
model.setKey(""+currentLocation);
model.setValue(currentLocationValue);
int position = cityAdapter.getPosition(model);
Position is returning as -1.
Can any one please guide me how to solve the issue.
You can easily get position of a particular item on click of a list,
is there any particular reason so that you don't want to get item position on list click?
Related
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 :)
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
}
I have
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, column));
getListView().setTextFilterEnabled(true);
and
public void onListItemClick(
ListView parent, View v, int position, long id)
I need to fine the position in column of the clicked item. Position is no good since the filter is on. Can I somehow derive the real position from 'id'?
One possible solution:
After you've set up a listview, do a once-through of the entire list, adding all the visible items indexes to an array. For instance, in a 5 element list, with the 2nd and 4th items not visible, it would look like this:
indexes[0] = 0
indexes[1] = -1
indexes[2] = 1
indexes[3] = -1
indexes[4] = 2
then when you get a position in the onListItemClick, that's just an index in the "indexes" array that returns the real position in the filtered ListView.
Then inside your onListItemClick, just use the position passed as a parameter as a key to look up the position in the filtered list.
EDIT: SparseArray is overkill. Offering reasonably easy solution.
I guess you can use .getChildCount() and .getChildAt() and then check .getVisibility() == View.VISIBLE on each to count the number of visible, and check if the clicked item is equal to the current iterrated. There's no straightforward way I'm afraid.
position in listview is the position of the element in generic object ie list you pass to an array adapter.
I haven't ever had good luck with ItemIDs.
Might want to instead pull the data from View v. This view might contain a TextView, which you could use to determine which row the user clicked on. This won't work if multiple rows can be displayed identically though...
So if that is the case, you are going to have to iterate through the dataset calling getItemID(int position) on every element in the adapter. Store these longs in an array the same size as the dataset, and use the same index on both your column dataset and this long ItemID array.
I have a ListView that uses a custom layout that has 5 child View objects (4 TextView and 1 CheckBox) in the custom layout. Only the TextViews are bound, however - the CheckBox has no corresponding field in the database. The purpose of the CheckBox is simply to identify which of the items being displayed I would like to process in "the next step". I'm using a custom ViewBinder to assign the text values correctly (because some of the values from the DB are dates, etc).
Part of the user interface is three buttons - 'All', 'None', and 'Invert' that I use to toggle the status of each item in the list. For the 'All' button, for example, I do this with the following code (which I now know is NOT correct - I include it to show what I'm trying to do):
ListAdapter la = getListAdapter();
ListView lv = getListView();
int iCount = la.getCount();
for(int i=0; i<iCount; i++)
{
View vv = lv.getChildAt(i);
CheckBox cb = (CheckBox) vv.findViewById(R.id.ledgerItemCheckBox);
cb.setChecked(true);
}
This works fine as long as the number of items in the list doesn't exceed the list size so that all items are always visible. When I exceed that number, though, the 'getChildAt' function sometimes returns null because (if I understand correctly) it isn't meant to return all of the items in the list, only those items that are visible, which results in a NullPointerException.
How can I properly set the CheckBox on all views, even if they aren't visible?
Create a pseudo binding to one of your TextView data, create an ArrayList of Boolean objects as your back end for the checkboxes. Please note an ArrayList may not be the best data structure for your application. Fill the ArrayList with booleans set to the initial value of the corresponding position of your Cursor's data set. When you're binding use the ArrayList as the back end data. When you select all, none, or invert, update the Boolean objects in the ArrayList then call notifyDataSetChanged() on your SimpleCursorAdapter.
Addition ah, the other half of the problem, yes, use the onClickListener for the CheckBoxes but use the same listener, don't create a new object for each. Use the getPositionForView() method to get your position and update the underlying data.
Hey. I have the following code:
final String text = (String) lt.getItemAtPosition(position);
db.removeCategory(text);
What I want to do is to remove a item from a ListView. The problem I have is that I only can remove the item that is in the first position of the list.
Is like the getItemAtPosition(0);
Why's that? Can somebody help?
Thanks
Bind your array to ArrayAdapter and use its remove method to remove particular object. Refer this link to get some understanding of how it works.
Remove ListView items in Android
Why don't you add items into a collection or data datable and then bind it to ListView.
In ste removal phase you delete the item from the collection/data table instead direct one from the ListView.