I have Autocomplete textviews in my custom adapter. when i write something in onTextChange listener, i want to get item position. I it gives all items positions. Please help me regarding this issue. Thanks!
Asked
Active
Viewed 4,727 times
3
-
you can get position when autocomplete textview gain focus. Use focusChangeListener for tha – Vivek Mishra Jan 18 '16 at 11:30
-
But i want to get position when user type something in its onTextChanged method. – Ankit Kamboj Jan 18 '16 at 11:32
-
Not getting what is actually issue. Can you please post your code – Saveen Jan 18 '16 at 11:34
-
add your code also to make things more clear – Vivek Mishra Jan 18 '16 at 11:35
-
holder.edt_markedPrice.addTextChangedListener(new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { Log.e("position::::", "position::::" + position); } public void afterTextChanged(Editable s) { } }); – Ankit Kamboj Jan 18 '16 at 11:38
-
check this..i want to get only edit text position when user types in ontextchange listener method. – Ankit Kamboj Jan 18 '16 at 11:38
-
if you want item position then why are you using textwatcher ? Use itemClickListener for getting the item position. – Surabhi Singh Jan 18 '16 at 11:42
-
I want to get item position when user type something on edit text or Autocomplete textview. – Ankit Kamboj Jan 18 '16 at 11:45
-
because i have to change edit text value and then to notify the activity for that particular position. Then i have to calculate the sum of total edit text values. – Ankit Kamboj Jan 18 '16 at 11:47
5 Answers
2
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
}
});

DIGITAL JEDI
- 1,672
- 3
- 24
- 52
-
-
implement textwatcher in custom adapter or in activity(if adapter data is assigned on oncreate method) – DIGITAL JEDI Jan 18 '16 at 11:46
-
because i have to change edit text value and then to notify the activity for that particular position. Then i have to calculate the sum of total edit text values.So i need position in ontextchangelistener method. – Ankit Kamboj Jan 18 '16 at 11:49
1
This is how you can get position of Item you typed in AutoCompleteTextView. But you have to add the full name of item like "apple" or "apples" then this code will give you the position of that particular item. But its very expensive process if you have lots of items in your list.
public class Test extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextView;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chintan_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
list = new ArrayList<>();
list.add("Apple");
list.add("Apples");
list.add("Banana");
list.add("Aunt");
list.add("Orange");
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompleteTextView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
for (String string : list)
if (string.toLowerCase().equals(s.toString().toLowerCase())) {
int pos = list.indexOf(string);
Toast.makeText(Test.this, "" + pos, Toast.LENGTH_SHORT).show();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
Let me know if this code helps you.

Chintan Bawa
- 1,376
- 11
- 15
0
Use textwatcher, implement it in activity class.
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}

DIGITAL JEDI
- 1,672
- 3
- 24
- 52
0
hi can you please check there is some issue
AutoCompleteTextView tv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
final ArrayList<Student> list = new ArrayList<MainActivity.Student>();
list.add(new Student("abc"));
list.add(new Student("abc"));
ArrayAdapter<Student> adapter = new ArrayAdapter<MainActivity.Student>(
this, android.R.layout.simple_dropdown_item_1line, list);
tv.setAdapter(adapter);
tv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Student selected = (Student) arg0.getAdapter().getItem(arg2);
Toast.makeText(MainActivity.this,
"Clicked " + arg2 + " name: " + selected.name,
Toast.LENGTH_SHORT).show();
}
});
for more checking you can go from here...
0
you can use addTextChangedListener for this purpose
yourAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
} });

Nauman Ali Shah
- 163
- 11