I have two autocompletetextview
the goal for example if any one select the autocompletetextview "HUB ID " position#3 then the another autocompletetextview " HUBName" must be forced to be on position 3
i.e want the both of them at the same position
// HUB Name listener
HubName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
HubName.setSelection(pos);
Log.d("ID Array follow pos","HUB ID in selected");
HubID.setSelection(pos);
HubName.setSelection(pos);
ShltrArray.clear();
ShltrArray.add(Shltr1Array.get(pos).toString());
if (Shltr2Array.get(pos).toString() != "Not")
{
ShltrArray.add(Shltr2Array.get(pos).toString());
}
ShltrAdapt.notifyDataSetChanged();
}
});
HubName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
HubName.setSelection(position);
HubID.setSelection(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
HubName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HubName.showDropDown();
Log.d("ID Array follow ID ", Integer.toString(HubID.getListSelection()));
Log.d("ID Array follow Name ", Integer.toString(HubName.getListSelection()));
}
});
the solution to index the array list to the same position of the first array then inside autocompleteTextView listener then set text to this position as follow
HubName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
HubID.setText(IDArray.get(pos).toString());}
Related
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);
}
}
}
I have a Material Spinner which displays an image and text, but i want to retrieve just the text in OnItemSelected. But it is returning null.
My Adapter Class
public class SpinnerCustomAdapter extends BaseAdapter {
Context context;
int cat_pics[];
ArrayList<String> categoryNames;
LayoutInflater inflter;
public SpinnerCustomAdapter(Context applicationContext, int[] cat_pics, ArrayList<String> categoryNames) {
this.context = applicationContext;
this.cat_pics = cat_pics;
this.categoryNames = categoryNames;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return cat_pics.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(cat_pics[i]);
names.setText(categoryNames.get(i));
return view;
}
}
Setting the Adapter in Main Activity
customAdapter=newSpinnerCustomAdapter(getApplicationContext(),cat_pics,allCatNames);
spCategories.setAdapter(customAdapter);
spCategories.setOnItemSelectedListener(this);
onItemSelected
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(), "Selected Item : " + customAdapter.getItem(position) ,
Toast.LENGTH_LONG).show();
}
In your SpinnerCustomAdapter, your getItem() method returns null
#Override
public Object getItem(int i) {
return null;
}
Update getItem() method as below:
#Override
public Object getItem(int i) {
return categoryNames.get(i);
}
#. You can also use parent.getItemAtPosition(position) to get get selected item.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Selected Item : "
+ parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
Try the following:
spinner.getSelectedItem().toString()
(Or)
Inside OnItemSelectedListener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("SeletedItem",adapterView.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Try like this, may help you
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String text = spinner.getItemAtPosition(i)
Toast.makeText(getApplicationContext(), "Selected : " text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name);
holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name);
arrayAdapter=new ArrayAdapter<String>(rowView.getContext(), android.R.layout.simple_spinner_item,teacher_name);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner_teacher_name.setAdapter(arrayAdapter);
holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show();
}
});
You should be used selected listener, not click listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
String selected_item = parent.getItemAtPosition(pos).toString();
Log.d("TAG", "_log : item_selected : " + selected_item);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event.
To define the selection event handler for a spinner, implement the AdapterView.OnItemSelectedListener interface and the corresponding onItemSelected() callback method.
See documentation.
Use:
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked " + class_room[position], Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Instead of:
holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show();
}
});
Is there a way that I can get the previously selected item from a ItemSelected event on a spinner? My first thought was to respond to the OnItemClickedEvent, but that method is not supported for spinners.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
MyItem current = (MyItem) parent.getItemAtPosition(position);
//MyItem previous = (MyItem) parent.getPrevious ... how
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//do nothing
}
});
I'm aware I can just cache the previous value, but it seems like a common operation that there should be a method for.
Use a variable to track the previous item. It would likely be at member of the Activity.
MyItem previous, current;
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
previous = current
current = (MyItem) parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//do nothing
}
});
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
int prev = comboValues.getSelectedItemPosition();
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
prev=position;
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
}
);
Note that nothing happens if you select an item you have already selected
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// my code here, but I don't know how..
}
}
I think it will be something like this:
get the selected item position of mySpinner
switch(pos)
Update spinner entries? I don't know...
I'm not sure what to do for the last step. Help?
try in this way
car.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
itemSelected = arg2;
// add items to the cardetails spinner's adapter using the itemselected and refresh the adapter using nofifyDataSetChanged()
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
cardetails.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
here car and cardetails are spinners
you need to refresh the adapter that is related to the car spinner so that the spinner also gets refreshed