So I've been following some advice around similar questions and I somehow couldn't manage to get into it. I have a spinner called 'e' from which I want to retrieve the selected string when the user clicks a button below it.
I implemented the nested class like this:
class SelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent){
//does nothing
}
}
e.setOnItemSelectedListener(new SelectedListener());
Just following the advice from other users around here and the android developers tutorial, the thing is, after this, I don't know which call should I make to retrieve it if I want to save it into a String variable like so:
String selected = //don't know what to put here
Hope is clear enough. Thanks in advance.
I'm pretty sure:
e.getSelectedItem().toString()
Related
Hi guys this is rather a silly question because i'm confused. what i'm trying to do is to call a method(share to facebook) from an activity inside my adapter.
this is the illustration:
Activity:
postToFacebook(Pets dog);
listview that holds list of dogs.
Adapter:
adapter in which i use to create custom view for better looking.
problem is that i want to set an onLongClickListener on the LinearLayout that holds all the dog's info (this way user can hold any part of the info) to be shared to facebook.
is there a way to do this?
thanks a lot!
If I understand your question you are complety on bad way
In your activity you have an ArrayList (or List) with Dogs information
Something like
ArrayList<Pets> listDogs;
now, you should do this (myListView is your ListView obj)
myListView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
postToFacebook(listDogs.get(pos));
return true;
}
});
In this way you get the position of element selected and call your method from activity and not from adapter.
listDogs.get(pos)
this line return the element of the row selected.
I used to dynamically populate a Table Layout with different elements per row, among them a Spinner.
To handle the initialization of spinner values i used what most recommended: a boolean flag variable. The code went something like this
public class spinnerHandling implements OnItemSelectedListener{
public void dynamicallyPopulateTableLayout(int a){
...
//initialize spinner with default values
mySpinner.setSelection(a);
mySpinner.setTag(true);
mySpinner.setOnItemSelectedListener(this);
...
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(!(Boolean)parent.getTag()){
//my code
...
}
parent.setTag(false);
}
}
This worked fine.
Later i decided that i should use a List View instead of a Table Layout. For that i have a custom ArrayAdapter that uses a row Layout, in which of course i have a Spinner, to populate the List View. As those of you who are familiar with List Views, understand its complex dynamic behavior, would know that the somehow static turn around of the boolean flag method wont work.
After three days of tremendous headache trying to find a way to resolve this issue, i just came across the spinner's isDirty() method that in contrast with isPressed(), isSelected(), etc. actually changes its boolean values when spinner item is selected by user.
the code is something like this:
public class spinnerHandling implements OnItemSelectedListener{
public void dynamicallyPopulateListView(int a){
...
//initialize spinner with default values
mySpinner.setSelection(a);
mySpinner.setOnItemSelectedListener(this);
...
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(!parent.isDirty()){
//my code
...
}
}
}
This seems to work fine!
But i would like to make sure that isDirty() handles properly spinner initialization and user selection.
Can you please confirm or contradict this behavior?
Thank you.
Let me explain myself:
As you know, when you've a view which have to be inflated several times, but changing values, you use a GridView or a ListView. Those two Composite views, have some methods like onItemClick. This method is so useful, as it returns the position of the view clicked.
With this position you can perform some concrete tasks, like retreiving from an ArrayList, the information of that object. Here's an example:
ArrayList<DocumentInfo> documents;
And when you set a setOnItemClickListener() you can get the correct values:
gallery.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
getDocumentInfoOf(pos);
}
});
public void getDocumentInfoOf(int position){
DocumentInfo doc = documents.get(position);
}
However, when you aren't using a GridView or a ListView, you're in your own. You don't have a clear way (AFAIK) to know which layout inflated is the one clicked (I mean like the previous example, the "position" value).
What I am currently doing, is the following:
for (int i=0; i<10;i++){
RelativeLayout documentInflated = (RelativeLayout) this.mInflater.inflate(R.layout.open_document_per_inflar, null);
documentInflated.setContentDescription(""+i);
documentInflated.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
openDocument(v);
}
});
container.addView(documentInflated);
}
public void openDocument(View v){
int idDocument = Integer.parseInt(v.getContentDescription());
//idDocument is the view clicked
}
Do you guys think this is a clear way of doing this?
Thank you!!!
If I'm not mistaken you want to get some data from your created Relative Layout when you click on it. The best solution here is to use the method setTag(Object tag). After that you get the informatiom with the method getTag(). This method allows you to add extra information to your view. As it says in the documentation:
Tags
Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
Also depending on your needs you can seperate every tag with a key -> value pair with the method setTag(int key, Object tag), after that you can retrieve this object with getTag(int key);
So in your case you will have
documentInflated.setTag(i)
in the onClick yo will then have:
int i = (int)v.getTag();
Unless I'm not understanding your desire...
#Override
public void onClick(View v) {
openDocument(v);
}
v IS the view being clicked. Your code looks like it should do what you're hoping it will do. What are you actually seeing happen?
So my situation is this. I have two tables. A main table where data is added using a form, this form has a spinner which is powered from the second table (this part is working correctly). I then want the selected item in the spinner to be stored in a foreignkey field in the main table.
I have been using the following code:
private Spinner mSpinner;
mSpinner = (Spinner) findViewById(R.id.spinCat);
and in my populate fields method I have the following:
mSpinner.getSelectedItem();
but this stores something like "android.database.sqlite.SQLiteCursor#43e5be60" in to the database rather than the name.
If anyone can offer some help it would be great, this has been bothering me for some days now and has halted development somewhat. If you need to see more code, then please don't hesitate to ask.
Any help is much appreciated.
EDIT: new code:
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View vw,
int pos, long id) {
LinearLayout rowll = (LinearLayout) vw;
TextView test = (TextView) rowll.getChildAt(0);
Toast t = Toast.makeText(parent.getContext(), test
.getText().toString(), Toast.LENGTH_SHORT);
t.show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do nothing..
}
});
getSelectedItem returns an Object (which is what your output looks a lot like).
Did you try simply calling toString like this?
mSpinner.getSelectedItem().toString();
I personally just set a variable in the onItemSelected listener, but this should work too. What I use is:
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
myIP = parent.getItemAtPosition(position).toString();
}
Variable is then continuously updated and I just add it to the DB when I connect to the IP in question.
Cursor data extraction:
while (cursor.moveToNext()) {
ipList.add(cursor.getString(1));
}
getString(int columnIndex) does: Returns the value of the requested column as a String.
http://developer.android.com/reference/android/database/Cursor.html
Don't use .getChildAt(), use .getItemAtPositon(). See how the code does it at http://developer.android.com/resources/tutorials/views/hello-spinner.html.
I am using a custom list view. I have tried to get the value of the row which the user clicked.
Can anybody tell me how to get the value?
Thanks
You may want to implement a new method in your class, specifically onItemClick:
[...]
private ListView lv;
[...]
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
String itemValue = lv.getItemAtPosition(position);
[... do something ...]
}
Then you can do whatever you want with with itemValue.
Hope this helps.