I've create the android application program using list adapter that extends the list activity....Inside the list i am having the spinner.. how can i get the value from the spinner inside the list..tell some idea.Thanks in Advance..
try this ::
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String str = mSpinner.getSelectedItem().toString();
// getting item id by this
myId = parent.getItemAtPosition(position).toString();
}
I am assuming you only have one spinner in the list. In that case you can get the view by id of that spinner and fetch the value. If there are more than one then you need to add different tags to them while adding them to the list (in ListAdpater) and get these views from tag and get the value.
Related
My ListView looks like this:
[CheckBox] [TextView]
My question is, how can I change the item position when CheckBox is checked? I mean to say, if the user checked any item from ListView, then the checked item goes to the end, so its position changes from current to last.
Thanks in Advance.
Use Custom adapter for listView. here is an example. Now from your ListActivity class
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, Object> map = (HashMap<String, Object>)
lv.getItemAtPosition(position);
// get the item Position from here and make the nacessary changes
// and update the listview again.
}
Without posting any code, this will have to be a brief overview of the pseudo steps you need to take
You simply need to update the ordering of your data set being used by your adapter (usually an arraylist or array of objects), and then called
notifyDataSetChanged()
on your adapter.
So in your case, you want to take the element at the position that was clicked, and put it at the end of your arrayList of objects.
If you post some code, you may get more detailed answers however
Here is the step you can follow. I have not tested it but you can try this.
// your value array you are binding with listview
private final String[] values;
// put default value to "n". size will be the same as for values array.
private final String[] valuesChecked;
onClick of Checkbox Listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get checkbox is checked or not
if it is checked then{
valuesChecked[position]="y";
}else{
valuesChecked[position]="n";
}
// short your values array by first "n" and after "y".
// call your adapter.notifyDataSetChanged();
}
Good Luck :)
As the question suggests, is it possible to identify the elements of a listView using string ids or something similar to id? I know the typical signature is
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {...}
But I need to have string ids or such. How would I do that?
Elaboration (sorry if still unclear)
The problem is my underlying list is a rotating list (i.e. circular list)
myList.addFirst(myList.deleteLast());
So the id/index of an item keeps actually floating around. But I need an id that is essentially a part of the element so that no matter the rotation, I can always retrieve the exactly element I mean to.
Create an Array List of strings. Add the strings id for listview in it.
Add a hidden textview in your listitems.
Add all the string id from Array List to your listview hidden textview.
Access the string id as below :
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
TextView anyId = (TextView) view.findViewById(R.id.anyID);
String val_Id = anyId.getText().toString();
Using ArrayList allows to handle dynamic listview.
You can create an array of string ids for your listview. In onItemClick, you can use the position to assess your string array. As the list items and your string will be of same size, it should have one to one mapping.
I hope you understand what I mean..
If you want to have a String row ID kindly get the position then convert it into string.
I have a custom ListView with two TextViews both containing different values. What I want to be able to do it get the contents from one of these TextViews when an item is clicked.
This is the code I have so far:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value;
// value = (get value of TextView here)
}
});
I want to be able to assign value to the text of one of the TextView's.
Although #Sam's suggestions will work fine in most scenarios, I actually prefer using the supplied AdapterView in onItemClick(...) for this:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person person = (Person) parent.getItemAtPosition(position);
// ...
}
I consider this to be a slightly more fool-proof approach, as the AdapterView will take into account any header views that may potentially be added using ListView.addHeaderView(...).
For example, if your ListView contains one header, tapping on the first item supplied by the adapter will result in the position variable having a value of 1 (rather than 0, which is the default case for no headers), since the header occupies position 0. Hence, it's very easy to mistakenly retrieve the wrong data for a position and introduce an ArrayIndexOutOfBoundsException for the last list item. By retrieving the item from the AdapterView, the position is automatically correctly offset. You can of course manually correct it too, but why not use the tools provided? :)
Just FYI and FWIW.
You have a few options. I reference the code from your previous question.
You can access this data from the row's layout view:
ViewHolder holder = (ViewHolder) view.getTag();
// Now use holder.name.getText().toString() and holder.description as you please
You can access the Adapter with position:
Person person = mAdapter.getItem(position);
// Now use person.name and person.description as you please
(By the way in your Person class, name and description are public so you don't need the get methods.)
Override following method in adaterclass.
public String[] getText() {
return text;
}
this example: http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html .There are 3 textview ids at custom_row_view.xml. There are 3 row data inside one position listitem when using onListItemClick. how to extract these data? how to use the id? Anyway to get row view data in the list view when the list item is clicked
[protected void onListItemClick(ListView l, View v, int position, long id]?
The parameter View v itself is the row view. You can get the attached data by calling v.getTag(). Should set it earlier in getView of adapter using v.setTag(Object)
Depends on the kind of data. For example id does contain the _id from a database table row that was set with one of the CursorAdapters. Usually this is the PK of that database table row.
listView.getAdapter().getItemAt(position)
gets you the object bound to the view V
I would like to recommend you not to get data from the view, instead use the ArrayList you have used to set data to the Adapter of the ListView.
In the example which you have pointed out, you are using an ArrayList of HashMap. So for an example..
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// arrayList is the variable which you have used as a list in your SimpleAdapter
hashMap = arrayList.get((int)id); // you need to typecast 'id' from long to int
value = hashMap.get(KEY);
}
});
I have an array of Strings I'm populating a Spinner object with. However, I'd like to attach an ID to each element of the Spinner, so when the user selects an item, I have its ID to use to save to some other piece of data. How can I do this?
Create a class StringWithTag and use in place of the string name in the list like so :-
public class StringWithTag {
public String string;
public Object tag;
public StringWithTag(String stringPart, Object tagPart) {
string = stringPart;
tag = tagPart;
}
#Override
public String toString() {
return string;
}
}
in the add items to spinner part :-
List<StringWithTag> list = new ArrayList<StringWithTag>();
list.add(new StringWithTag("Oldman", "12345"));
list.add(new StringWithTag("Umpire", "987654"));
list.add(new StringWithTag("Squad", "ABCDEE"));
ArrayAdapter<StringWithTag> adap = new ArrayAdapter<StringWithTag> (this, android.R.layout.simple_spinner_item, list);
....
....
in the listener :-
public void onItemSelected(AdapterView<?> parant, View v, int pos, long id) {
StringWithTag s = (StringWithTag) parant.getItemAtPosition(pos);
Object tag = s.tag;
}
voila!
}
What do you mean by id. You can use ArrayAdapter to populate the Spinner. When item is selected just get the element from the adapter and save the data you want.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<MyObject> adapter = ... // initialize the adapter
adapter.setDropDownViewResource(android.R.layout.some_view);
spinner.setAdapter(adapter);
and when item is selected
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
MyObject selected = parent.getItemAtPosition(pos);
// save any data relevant with selected item
}
If you are storing your data in db you can use CursorAdapter and in onItemSelected to fetch the selected item id from the cursor.
I don't think you can attach an arbitrary ID to elements of a text array resource, if that's what you're using.
I think the simplest way to attach such an ID would be to either hard-code (if you're using a static text resource) or dynamically build (if you get the strings at runtime) a mapping from (String position in array)->(primary key).
EDIT: On the other hand, Mojo Risin has a point - you should check to see if the CursorAdapter API already does what you need for you.
Andrew Hi, it's been a long time but it's worth to write.
You can set a tag for each row when you'r inflating spinnerLayout in SpinnerAdapter:
spinnerView = inflater.inflate(spinnerLayout, parent, false);
spinnerView.setTag("Your Tag");
And then you can get the tag with:
yourSpinner.getSelectedView().getTag();
I think The best solution is to add one more spinner and fill it with the ids but make the visibility of it to gone