I'm trying to get the value of an item that is inside a Spinner, and then I want to send it to a database.
However, the code:
String variable = spinner_name.getSelectedItem().toString() does not work at all
So it seems the best way is through of the method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_LONG).show();
}
But how do you get the value of this variable called text, which is inside the above method, to insert into another method?
Create a CustomAdapter for your spinner, pass a callback as parameter when creating the customAdapter. Once you click on a item on spinner, because of callback, you'll have the item on your frag/activity.
From there you can call any method you want.
Related
The following code returns a null value. Can anyone please tell me why? I'm looking to use the returned value to send it to another activity in an intent the Choice string is declared in the class this code belongs to:
public String SetupBreakfastSpinner()
{
Choice = "";
SpinnerSelector= ArrayAdapter.createFromResource(HomeActivity.this, R.array.breakfastArray, android.R.layout.simple_spinner_item);
SpinnerSelector.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Breakfast.setAdapter(SpinnerSelector);
Breakfast.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Choice= parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return Choice;
}
In method SetupBreakfastSpinner you are initializing Choice to empty string. When you call this method you are setting adapter to spinner correctly but immediately returning Choice which is still empty.
As you have to send Choice value to another activity, you have two options:
Approach 1: When user choose an Item in spinner, onItemSelected callback gets called:
void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Choice= parent.getItemAtPosition(position).toString();
//start your activity here, pass Choice value in the Intent
}
Approach 2: Assuming that you have some button, which when clicked you start activity, in onClick of button you can start activity
void onClick(View v){
//Start activity, pass Choice in Intent
}
Keep in mind that values that changes on certain event, for example
here Choice variable gets updated when user choose values, you need
to be careful before using such variables, such variable might be
null.
I'm using an SQLite database to populate a listview. I've made it so that the user can not pass the data to the listview until a TextView has been populated.
Now I'm trying to get the TextView, which is defined in XML, to be populated by having the user click a button that should then display text on the TextView. However, I do not know how I can pass it to the TextView when it's not made programmatically.
So how can I pass the data (packageName) to TextView?
If relevant, my onItemClick looks like this:
myGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Putting package name into packageName
final String packageName = mApps.get(position).activityInfo.packageName;
ResolveInfo info = mApps.get(position);
info.activityInfo.loadLabel(getPackageManager());
Toast.makeText(getApplicationContext(), "Clicked " + packageName,
Toast.LENGTH_SHORT).show();
}
});
You would do need to declare your text view then put this inside the OnItemClick method.
myTextView.setText(packageName);
I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?
The parameter v is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(Or you could use getChildAt(position) but this would be slower.)
Understand you might be able to simplify this more depending on your layout.
Just Small Change is Required
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
i.putExtra("hello",adapter.getItem(position).getUsername() );
startActivity(i);
}
});
Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.
I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy)
Otherwise you might end up using "public static" variable, which might be a memory leak...
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.
i have two spinners.
If in the first one the Item "Diesel" is selected i want to display the second one.
sKraftstoffArt.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(sKraftstoffArt.getSelectedItem().toString() == "Diesel"){
sPartikelfilterArt.setVisibility(sPartikelfilterArt.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
I've implemented this code in the onCreate method. When i select a item during the runtime i'm not getting the selected item text... It works only if the activity gets created and the default value gets selected....
Where else do i have to implement it?
Regards,
float
Unless your sKraftstoffArt object is not a final one, the check against it's selected item text inside an anonymous class won't work.
The adapterView among the parameters is your ListView instance to which you've assigned the AdapterView.OnItemClickListener.
The view parameter is the actual item (renderer) inside your ListView that has been clicked. This item is provided by your adapter's getView(int position, View convertView, ViewGroup parent) method.
Also, you should use the equals method of String to check whether two String values are equal.
So this won't work:
if(sKraftstoffArt.getSelectedItem().toString() == "Diesel")
Use insetad
if (adapterView.getSelectedItem().toString().equals("Diesel"))
You might also want to add an else clause after this if, to hide the sPartikelfilterArt spinner when the selected item in the previous spinner is not "Diesel".
Please note, that every time you assign a new adapter to this list (which probably you don't, i still mention it just in case...), you should add the AdapterView.OnItemClickListener to it again.