How to use autocomplete textview - java

When I fill name and select in auto-complete text-view. It's not working same spinner. function in if-else not working or condition incorrect(I don't know).Please suggest me how to use onitemselectlistener on auto-complete text-view to use function in each condition.
AutoCompleteTextView auto1 = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
String[] word;
word = getResources().getStringArray(R.array.word_name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, word);
auto1.setAdapter(adapter);
auto1.setOnItemSelectedListener(new OnItemSelectedListener() {
TextView txt1 = (TextView)findViewById(R.id.textView1);
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(arg2==0) // when I fill AED(array 0 in string.xml It's not work)
{
getmoney();
}
else if(arg2==1)
{
getmoney1();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
String.xml
<string-array name="word_name">
<item>AED United Arab Emirates Dirham</item>
<item>AFN Afghan Afghani</item>
<item>ALL Albanian Lek</item>
<item>AMD Armenian Dram</item>
</string-array>

Instead of OnItemSelectedListener, OnItemClickListener works for AutocompleteTextView.
For example:
auto1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
adapter.getItem(position).toString(),
Toast.LENGTH_SHORT).show();
}
});

Related

how to set image in item recyclerview when user choose a item in spinner

i have a spinner in add new data, I want the selected data to be an image in the item of recyclerview anyone knows how to make like that
like this but spinner that is from activity not item recycler view and thats the problem, how to get id spinner in adapter
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
if (item == important){
holder.img_category.setImageResource(R.drawable.important);
}else if (item == notImportant){
holder.img_category.setImageResource(R.drawable.notimportant);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
use Glide for image set in imageView check below code.
add -> 'implementation 'com.github.bumptech.glide:glide:4.9.0' into your app.gradle
yes check that item getting your desired values using log and use
if(item.equalsIgnoreCase(important)) to check your if condition.
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
log.e("TAG","Selected ITEM :"+item);
if(item.equalsIgnoreCase(important)){
Glide.with(context).load(R.drawable.important).into(holder.img_category); //here
}else if(item.equalsIgnoreCase(notImportant)){
Glide.with(context).load(R.drawable.notimportant).into(holder.img_category); // here
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Hi look updated answer
selectImageType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
if(i!=0)
{
selectedItem=binding.selectImageType.getSelectedItem().toString();
Log.e(TAG, "onItemSelected: "+selectedItem);
if(!selectedItem.equalsIgnoreCase(""))
{
spinnerDemoAdapter=new SpinnerDemoAdapter(dataImageList,selectedItem,SpinnerDemoActivity.this);
binding.recycleSpinnerDemo.setHasFixedSize(true);
binding.recycleSpinnerDemo.setLayoutManager(new LinearLayoutManager(SpinnerDemoActivity.this, RecyclerView.VERTICAL,false));
binding.recycleSpinnerDemo.setAdapter(spinnerDemoAdapter);
spinnerDemoAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And this below is your adapter code
I take arraylist here and add random values for testing purpose
public SpinnerDemoAdapter(ArrayList<String> arrayList, String selectedItem, Context mContext) {
this.arrayList = arrayList;
this.selectedItem = selectedItem;
this.mContext = mContext;
}
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
if (!selectedItem.equalsIgnoreCase(""))
{
Log.e(TAG, "onBindViewHolder: "+selectedItem);
if (selectedItem.equalsIgnoreCase("important")) {
Glide.with(mContext).load(R.drawable.woman).into(holder.imageview);
} else if (selectedItem.equalsIgnoreCase("notimportant")) {
Glide.with(mContext).load(R.drawable.man).into(holder.imageview);
}
}
}

Reading value from String Array populated using ListView

I've got a ListView which populates a list of quotes from a String Array, however onClick I require the selected <item> to be read to a String. This is the code I use:
ListView sample = (ListView)findViewById(R.id.Samplelist);
String[] backup = getResources().getStringArray(R.array.months);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, backup);
sample.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
#Override
public final void run()
{
String readItem;
finish();
}
});
}
sample.setAdapter(adapter);
}
And here sample.setAdapter(adapter) seems to be an error and is underlined with red.
Is there a way to get this done correct? Any help will be appreciated
If you look at layout of simple_list_item_1 then you can find id of textview is text1.
You can define OnItemClickListener to your listview like this:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = ((TextView) view.findViewById(R.id.text1)).getText().toString();
}
});
EDIT:
You can also try
String selectedItem = (String)parent.getItemAtPosition(position);
instead of
String selectedItem = ((TextView) view.findViewById(R.id.text1)).getText().toString();
Hope it helps!
You can try this approach:
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String data=(String)parent.getItemAtPosition(position);
}
});
You may need to parse the text from View in OnItemClickListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = ((TextView)view).getText().toString();
}
});

Retrieve Object From Spinner Array And Set It

I'm trying retrieve an object from my spinner array and set it to a certain constant, in this case "EFFECT_AQUA"
My array
String[] spinnerValues = {"Aqua", "Mono", "Blackbird", "Negative"};
when the user clicks on the "Aqua" in the spinner I want the screen to change to Aqua.
My spinner is set and called
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this,R.layout.custom_spinner,spinnerValues));
But not sure how should I approach. Seen many different answers but haven't found anything working.
I know my switch will come in this part
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
Any help is appricated!!
If you want to retrieve an object from your spinner array on item click, you can easily do it with the position parameter in the method onItemSelected.
For example :-
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelected = spinnerValues[position];
//Set this to a constant
}
}
Now if you select Aqua, then code will set the variable itemSelected to Aqua.
I hope this is what you need
final Spinner cardStatusSpinner1 = (Spinner)findViewById(R.id.text_interested);
String cardStatusString;
cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
cardStatusString = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
put the code below to get the item first then you can manipulate by taking the values like aqua or any color.
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,spinnerValues));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view;
Toast.makeText(getApplicationContext(), tv.getText().toString(), 5000).show();
switch(tv.getText().toString()){
case "Aqua":{
//change the color to aqua
break;
}
case " ...":{
//
break;
}
//.... for all the option
}
}

Click on spinner element android

I created a spinner in my main.xml:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/categoria_arrays"
android:prompt="#string/categoria_prompt" />
And in the strings.xml the values:
<string name="categoria_prompt">Choose</string>
<string-array name="categoria_arrays">
<item>All</item>
<item>One</item>
<item>Two</item>
<item>Three</item>
</string-array>
I can display it normally but actually there are no interactions.. I need that onClick over a item open a new activity for example. So if i click the item at position 2 i need go in the activity One. Is it possible?
I tried to create a toast when i click a item but not works:
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " +
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
I How could i do?
Simply call something like this in your onCreate after your setContentView:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes it is possible. Do it like this
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
if (pos == 1){
Intent i = new Intent(currentActivity.this, One.class);
startActivity(i);
}else if (pos == 2)
{
Intent i = new Intent(currentActivity.this, Two.class);
startActivity(i);
}else if (pos == 3){
Intent i = new Intent(currentActivity.this, Three.class);
startActivity(i);
}
}
and so on...
You need to set item selected listener like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
You need to explicitly set the listener for the spinner.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
// your code here
}
public void onNothingSelected(AdapterView<?> arg0) {
// your code here
}
});
Did you just added onItemSelected method, or setted onItemSelectListener? onItemSelected and onNothingSelected are just methods, you need to use onItemSelectListener.

unable to retrieve value from spinner

This is a very basic issue.
It is that, I have a basic Spinner implemented with a listener as below:
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array, android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();
The issue is the sMode variable result is NOT appearing in the Toast message(value is NULL) where as it works fine within the OnItemSelected method.
Where am I going wrong?
you are executing the Toast.show(); method before the sMode value could be set, instead place this in onItemSelected() after setting sMode value, change your code to following:
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array,
android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(),
"You need to select atleast one mode!",
Toast.LENGTH_SHORT).show();
}
});
You can do this
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array, android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
//CALL_YOUR_FUNCTION_HERE
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
}
});
some time may be it is silly mistake so try this..
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// sMode = parent.getItemAtPosition(pos).toString();
sMode = parent.getAdapter().getItemAtPosition(pos).toString();
}

Categories