Android: Autocompletetextview dropdown hides after entering space - java

I have created an AutoCompleteTextView drop-down which contains multiple words in a row like -
row 1 - Infosys Limited
row 2 - State Bank of India
when I'm entering Infosys, it works fine when I enter space after the drop-down goes off.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, GreenDaoUtils.getSSSymbolNamesArray(this));
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setLines(8);
autoCompleteTextView.setDropDownBackgroundDrawable(getResources().getDrawable(R.drawable.cust_rect_border));
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
});
}
});

Use FilterWithSpaceAdapter instead of ArrayAdapter
u get from FilterWithSpaceAdapter here
FilterWithSpaceAdapter<String> adapter1;
adapter1 = new FilterWithSpaceAdapter<String>(this, android.R.layout.simple_list_item_1, GreenDaoUtils.getSSSymbolNamesArray(this));

Related

how I can hide the element with index 0 from dropdown list in spinner

Please tell me how can I hide the item at index 0 from the dropdown in the spinner in Android Studio? I am using this code, it works, but when I open the list, it shows at the bottom. That is, it is focused on the elements below. what do i need to change?
SpinnerName = (Spinner) v.findViewById(R.id.spinner1);
ArrayList<String> names = new ArrayList<>();
names.add(0, "SELECT");
names.add(1, "Name1");
names.add(2, "Name2");
final int listsize = names.size()-1;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, names){
#Override
public int getCount() {
return(listsize);
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerName.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_list);
SpinnerName.setSelection(listsize);
SpinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
....
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
SpinnerName.setSelection(listsize);
your problem is here! you are passing the total list size number and
that where you are doing wrong, because setSelection method use to
show default index of spinner.
you can simply do that SpinnerName.setSelection(1); that would give you the fist spinner item unless showing the names.add(0, "SELECT"); item

how to get the items changed in spinner android studio

I want to change the value of the second spinner when the value of the first spinner is changed I tried the solutions but did not get an exact solution so anyone have a solution for that so please suggest or any question then please ask
ArrayAdapter<String> deptArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.Province));
deptArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvinceEyeTest.setAdapter(deptArrayAdapter);
deptArrayAdapter.notifyDataSetChanged();
Set a OnItemSelectedListener() first.
spinnerProvinceEyeTest.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString(); //this is your selected item
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
In onItemSelected() of above Listener, place your condition and set resource for second Spinner as:
If(selectedItem == "YourText"){
//Here, you're initalizing the second `Spinner`
ArrayAdapter<String> secondArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.YourList));
secondArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
secondSpinner.setAdapter(secondArrayAdapter);
}
else{
//Use any other list with arrayAdapter in case of other item selected.
//Use can use If-else-if or Switch to select different resource for different item of first spinner
}
Reference - Official Documentations.

Cannot Sucessfully Delete Item From ListView In Set Counterpart

I feel like this shouldn't be a complicated problem, but I've tried reworking it and cannot think of a solution as to why it isn't working.
I have a listView that adds a ArrayList notes to it. For this listView, when you longpress on an item, it creates a pop up that deletes the item clicked. my set notesSet takes data from sharedPreferences and adds it to notes and gets updated when notes changes. This deletion is controlled by a Yes/No box. For some reason, I cannot remove the item from notes and have the listView show the updated form.
Here is the code for the long click:
myPref.edit().putStringSet("NN", notesSet).apply();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
new AlertDialog.Builder(Schedule.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Pop Up!")
.setMessage("Ready to delete this task?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
SharedPreferences myPref = Schedule.this.getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);
Set<String> notesSet = new HashSet<String>(myPref.getStringSet("NN", null));
ArrayAdapter arrayAdapter = new ArrayAdapter(Schedule.this, android.R.layout.simple_list_item_1, notes);
ListView listView = (ListView) findViewById(R.id.listView);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
notesSet.remove(i);
notes.clear();
notes.addAll(notesSet);
myPref.edit().putStringSet("NN", notesSet).apply();
listView.setAdapter(arrayAdapter);
/*
notes.remove(i);
notesSet.addAll(notes);
notes.clear();
*/
}
})
.setNegativeButton("No", null).show();
return false;
}
});
I've actually pinpointed the problem, I think, to these three lines of code. Problem is, I can't figure out what it is I'm doing
notesSet.remove(i);
notes.clear();
notes.addAll(notesSet);
Actually it is a simple mistake, instead of passing String value to notesSet.remov(), you are passing integer value to it.
So make change like this
notesSet.remove(i)
to
notesSet.remove(notes(i))

Array position using onItemClickListener

I'm trying to get the position of an array using the onItemClickListner.
With the following code, I am able to pass the text of the item clicked but not the position of the array eg 0, 1, 2 etc.
String[] menuItems = new String[]{"Hello", "Is it me", "Youre", "Looking for"};
ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.menulists, R.id.menulistsTextView1, menuItems);
ListView listView = (ListView) findViewById(R.id.mainListView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
String selected = (String) l.getItemAtPosition(position);
}
Any tips on whether it is possible to do that, I tried changing String to int and also to use the getItemIDAtPosition function but would not work, would just close the application.
Cheers
EDIT
I have just taken a different approach to achieving what I want to do using if statements for the string of the item clicked. Thanks for the input
You can get the position from method signature :
public void onItemClick(AdapterView<?> l, View v, int position, long id)
position give you index of selected item.
you may try this :
String itemClicked = menuItems[position];
As I understand Your problem - You want to get item on clicked position. Do do so use getItem(position) method of adapter. Code:
String[] menuItems = new String[]{"Hello", "Is it me", "Youre", "Looking for"};
//VERY IMPORTANT
//adapter must be declared final to use it in onItemClick
//or adapter should be object parameter
final ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.menulists, R.id.menulistsTextView1, menuItems);
ListView listView = (ListView) findViewById(R.id.mainListView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
String selected = <String>adapter.getItem(position);
}
});

how to change the string array of a spinner at runtimeHow to use the selected item of a spinner in an if statement (Android in eclipse)

Hi I have a spinner (spinner1) and when the user selects an item from it (example: "Canada" I want the spinner2 to populate with Provinces of "canada". I have tried the following but it doesnt work. I tried to do this by using:
if (spinner1.getSelectedItem().toString().equals("Canada"))
{
addItemsOnSpinner2();
}
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List list = new ArrayList();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
ArrayAdapter dataAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
for some reason the items aren't being added to spinner2. please help
This code
if (spinner1.getSelectedItem().toString().equals("Canada"))
{
addItemsOnSpinner2();
}
will only run the first time the Activity is created since it is in your onCreate(). You need to add this code to your onItemSelected() for spinner1. So you first need to implement an OnItemSelected listener to that spinner.
spinner1.setOnItemSelected(new OnItemSelected()
{
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id)
{
TextView tv = (TextView)v; // cast the View to a TextView
if ("Canada".equals(tv.getText().toString())
{
addItemsOnSpinner2();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Something like this should work. Although, you will probably be safer to use position instead of getting the text.
OnItemSelected Docs

Categories

Resources