0

I know that there are multiple posts in Stackoverflow addressing this query. However, for some reason I am still failing to extract the string from AutoCompleteTextView. I tried using the onItemClickListener for this purpose. I am unable to identify where I am going wrong.

The Code :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    addPurchaseItemName = (AutoCompleteTextView) findViewById(R.id.addPurchaseProductName);

    vivzHelper = new VivzDatabaseAdapter(this);

    String[] autoCompleteName = vivzHelper.getInventoryNameFilterBySupplierName(vivzHelper.getSupplierID(param1));
    ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
    addPurchaseItemName.setThreshold(1);// starts working from first char
    addPurchaseItemName.setAdapter(NameAdapter);

    addPurchaseItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            itemName = String.valueOf(arg0.getItemAtPosition(arg2));
        }
    });

}

I want to assign the value of the extracted string to itemName which is initialized at the beginning of the activity. Can some one point out where am I going wrong? I have surfed multiple resources. Maybe, I am missing something.

Note :

This code was already posted to address a an issue on IllegalArgumentException in StackOverFlow a couple of days ago. Since, the topic of that question doesn't point more specifically the problem posted here, I thought that posting a new question will make sense. Hence I hope, my question won't be down-voted or edited as duplicate

Update 01 : @Deividi Cavarzan forgot including the below line of code when editing this question

ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);

Update 02 : Declaring the itemName

public class AddPurchase extends ActionBarActivity {

AutoCompleteTextView addPurchaseItemName;
String itemName;
Community
  • 1
  • 1
user3314337
  • 341
  • 3
  • 13

2 Answers2

0

try to get the itemName on addTextChangedListener callback

addPurchaseItemName.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        itemName = s.toString();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {                

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});
Deividi Cavarzan
  • 10,034
  • 13
  • 66
  • 80
0

if you just want to get the selected item string from auto complete, then

itemName = addPurchaseItemName.getText(); 

or even better-

itemName = addPurchaseItemName.getText().toString().trim();

Updated answer

instead of setting onItemClickListener , set OnItemSelectedListener , this should definitely work.

addPurchaseItemName.setOnSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
        itemName = addPurchaseItemName.getText().toString().trim();
        Toast.makeText(getApplicationContext(), "selected value - "+itemName, Toast.LENGTH_SHORT);
    }
});
Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
  • I have already tried using `itemName = addPurchaseItemName.getText().toString()`. But, still shows an exception – user3314337 Apr 23 '15 at 08:18
  • what is the exception? can you post the logcat? the code snippet I answered works perfectly unless we have other problems. – Amit K. Saha Apr 23 '15 at 09:27
  • I am sorry. The second sentence isn't an response to you. Actually, there arises no exception in your case. However, the string value of `itemName` remains null which I have confirmed using a Toast. Sorry again. – user3314337 Apr 23 '15 at 09:48
  • Where are you putting the Toast?, do your onItemClick() get called at all? – Amit K. Saha Apr 23 '15 at 11:53