Im try to build auto Complete textview in android, and its work fine, but my problem is I need to get a value was related for item was selected, I mean
we have schools
abc, bbt, ccce, ddde
abc have id = 1, and number_of_students = 30
bbt have id = 2, and number_of_students = 20 students
ccce have id = 3, and number_of_students = 50 students
ddde have id = 4, and number_of_students = 40 students
when user write in text view abc, and select it, I need to get id and number_of_students, not only abc text,
I mean in html we have <option value="1">text<option>, when select it, we get a 1, not a text, is there option in android to set text and I get a value not a text
thanks a lot.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.schoolsAutoComp);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
//pos is the position of the selected item
Toast toast = Toast.makeText(getApplicationContext(), COUNTRIES[pos], Toast.LENGTH_LONG);
toast.show();
}
});
Final Solution:
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
String selection = (String) parent.getItemAtPosition(pos);
int pos2 = -1;
for (int i = 0; i < COUNTRIES.length; i++) {
if (COUNTRIES[i].equals(selection)) {
pos2 = i;
break;
}
}
System.out.println("Position " + pos2); //check it now in Logcat
}
});
setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id)
{
//pos is the position of the selected item
}
});
Related
String[] idArray = {"1","2","3","4"};
String[] nameArray = {"Abraham","Abhi","John","Joseph"};
final ArrayAdapter<String> adapterTextView = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameArray);
etItemName.setAdapter(adapterTextView);
etItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String id = idArray[position];
}
});
This is my code. In this, when I type "Jo", John and Jospeh are populating on AutoCompleteTextView and when I selected John I get String id as 0. But actually I need is 2. So how can I get 2 instead of 0?
This is not the perfect solution but you can take it as the last option:
When you pass the selected string just loop through your nameArray to find its position:
int position=-1;
for(int i=0;i<nameArray.length;i++){
if(selectedItem.equalsIgnoreCase(nameArray[i])){
position=i;
}
}
#Shaheer Palollathil n9153's suggestion would work. I'm just changing his answer according to your code.
AutoCompleteTextView t;
t.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = -1;
for(int i = 0; i < nameArray.length; i++){
if(((AutoCompleteTextView)view).getText().toString().equals(nameArray[position]))
pos = position;
break;
}
String id = idArray[pos]; // should be your desired number
}
});
This is my code so far. This is working but it only shows the very first value I have in the listView , not the selecting item by the user. Please halp me with this. This is really important.
unitListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
unitListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
TextView t1 = (TextView) findViewById(R.id.lblLUnitName);
String name=t1.getText().toString();
// String a= (String) unitListView.getItemAtPosition(position);
// touchpressed=position;
Toast.makeText(getApplicationContext(), "Selected Item :" +name, Toast.LENGTH_SHORT).show();
}
});
Update your code as follows and try ;
unitListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
unitListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
TextView t1 = (TextView) findViewById(R.id.lblLUnitName);
String name=String.valuesOf(position);
Toast.makeText(getApplicationContext(), "Selected Item :" +name, Toast.LENGTH_SHORT).show();
}
});
View is just to show some values on UI. If you want to get real values use position to get a value from your ListAdapter.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
YourItemClass item = (YourItemClass)listAdapter.getItem(position);
//[...]
}
Other way is to get ListAdapter from your unitListView directly:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = unitListView.getAdapter().getItem(position);
//[...]
}
Replace
TextView t1 = (TextView) findViewById(R.id.lblLUnitName);
By
TextView t1 = (TextView) view.findViewById(R.id.lblLUnitName);
I have an array (array1 with States) and AutoCompleteTextview in which I'm filling it with array1. When I select the value from AutocompleteTextView I select a state from AutoCompleteTextView Dropdown
What I want is to get the position of the item from array1 which I've selected.
What I've tried is OnClickEvent of AutocompelteTextView.
STATE.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String) parent.getItemAtPosition(position);
String num = selection;
}
});
It ss giving me the value which I selected from dropdown but i want to get the position of the value in array1. For Example I have an array of size 4, like array1 = {A,B,C,D}, and if I select B it should return me B position in Array i.e 2.
I hope I made it clear. Thanks in advance.
Use the position variable in the onItemClick.
STATE.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String) parent.getItemAtPosition(position);
int pos = -1;
for (int i = 0; i < yourarray.length; i++) {
if (yourarray[i].equals(selection)) {
pos = i;
break;
}
}
System.out.println("Position " + pos); //check it now in Logcat
}
});
Use List instead of Array. Implement onItemClickListener for AutoCompleteTextView, then use indexOf on your list to find the index of selected item.
actvCity.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int index = cityNames.indexOf(actvCity.getText().toString());
// Do Whatever you want to do ;)
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
TextView txtvw=(TextView) view;
String str=txtvw.getText().toString();
int index = contactNames.indexOf(str);
}
Item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//// id contains item if from database
ItemNoSelected = id;
}
});
id is from database, we can use that
You can find the parent string in the clickItem listener.
OSchools in this case is the custom ArrayList.
Object item = parent.getItemAtPosition(position);
for(OSchools d : data){
Log.e("name",d.getName());
String name = d.getName();
if(d.getName() != null && name.contains(item.toString())){
ID = d.getID();
}
I have an AutoCompleteTextView in my app which works. I have successfully created an onClickItemListener. The question is how to grab the text the user selected.
And this is the thing: I have an ArrayList with words being passed to the Adapter to search for suggestions. As the user types a word the suggestions list gets shorter (in rows on the UI side) so when i want to get the word from the ArrayList at the index the user selected i get the wrong word because the indexes doesn't match.
How can I get the text (String) the user chose without having to mess with the index?
Here's my code:
public class AutocompleteActivity extends BaseActivity {
private DBManager m_db;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete);
m_db = new DBManager(this);
final ArrayList<String> words = m_db.selectAllWords();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, words);
AutoCompleteTextView tv = (AutoCompleteTextView)findViewById(R.id.autocomplete);
tv.setThreshold(1);
tv.setAdapter(adapter);
tv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("SELECTED TEXT WAS------->", words.get(arg2));
}
});
}
}
Yeah... unfortunately the name of the parameters on the onItemClick method you must implement are not so self-descriptive but here is an example with the names of what they are:
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String)parent.getItemAtPosition(position);
//TODO Do something with the selected text
}
});
parent The AdapterView where the click happened.
view The view within
the AdapterView that was clicked (this will be a view provided by the
adapter)
position The position of the view in the adapter
id The row id of the item that was clicked.
For more info see: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
arg0 being your AdapterView and arg2 the position.
Have you tried:
arg0.getItemAtPosition(arg2);
I think what you are looking for is this.
String s = this.mCountry.getEditableText().toString();
Where mCountry is the AutoCompleteTextView.
this.mCountry = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(this, R.layout.list_item, countries);
this.mCountry.setAdapter(adapterCountry);
mCountry is the list of countries, and I wanted to save the country selected in SharedPreferences.
Hope this helps.
Easiest of all
For Getting text of the selected suggestion in AutoCompleteTextView use this
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("========>>", autoCompleteTextView.getText().toString());
}
});
try this:
txtPurpose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Purpose selected = (Purpose) arg0.getAdapter().getItem(arg2);
txtPurpose.setTag(selected);
}
});
One another of getting text of suggestion selected in AutoCompleteTextView is
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
TextView txtvw=(TextView) view;
String str=txtvw.getText().toString();
int index = contactNames.indexOf(str);
}
Here is the code that will solve the problem.
private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
int index = (int) view.getTag();
Object item = parent.getItemAtPosition(index);
if (item instanceof SearchItemShareConnectionDAO) {
SearchItemShareConnectionDAO dao = (SearchItemShareConnectionDAO) item;
}
}
};
SetTag(Dao.getPosition) in getView() method of adapter.
To get the text of the displayed item selected by the user
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selectedItemText = arg0.getItemAtPosition(arg2);
Log.i("myTag", "SELECTED TEXT WAS["+selectedItemText+"]);
}
Following best practices, please use some form of descriptive nomenclature for your variables, your code will make more sense:
public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int selectedItemIndexIn, long id)
There is also a way to get item outside the onItemClick:
int index = tv.getListSelection();
if (index != ListView.INVALID_POSITION) {
Object item = tv.getAdapter().getItem(index);
}
beware that int getListSelection() may return ListView.INVALID_POSITION if there is no dropdown or if there is no selection.
For me, but on setOnItemSelectedListener():
arg0.getSelectedItem();
was enough. The same should work on setOnItemClickListener() I guess.
Kotlin version
autoCompleteId.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position).toString()
Log.d("SELECTED ITEM", selectedItem )
}
Output:
D/CLASSROOM: selectedItem
I have this below code access the ListView item value into string and display it in alert?
ListView shot = getListView();
shot.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
String S = arg1.getContext().toString();
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage(S).show();
}
maybe this example will help you
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
https://developer.android.com/reference/android/widget/ListView.html
This gives you the exact value of the item clicked. Check the log
ListView shot = getListView();
shot.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
String val =(String) parent.getItemAtPosition(position);
System.out.println("Value is "+val);
}
To get the value of your model
adaptor.getItem(position).getCardName();
Maybe you can try this
String data = (String)shot.getItemAtPosition(arg2);
AlertDialog.Builder adb = new AlertDialog.Builder(arg1.getContext());
adb.setMessage(data).show();