0

I have a activity which has a button and 2 autocomplete widget. for the button i am using

addProductButton.setOnClickListener(this);

and for the 2 autocomplete widget i am using

supplierTextView.setOnItemClickListener(this);

now when i select the first autocomplete it runs this code but does not go into the if loop also when i click a item in the 2nd autocomplete it runs the same code but without going into the else

@Override
public void onItemClick(AdapterView<?> adapter, View view, int pos, long rowId) {
// TODO -

    String supName = supplierTextView.getText().toString();

    String proName = productTextView.getText().toString();

    System.out.println("Name selected  "+ view.getId());

    if(view == supplierTextView)
    {
        Log.d("Supplier Name selected", supName);
    }
    else if(view == productTextView)
    {
        Log.d("Product Name selected", proName);
    }

    loadProducts(supName);
    handleProductSuccess(filteredProduct);

}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
ZAJ
  • 793
  • 3
  • 23
  • 50

3 Answers3

0

The OnItemClickListener is used to listen for clicks on autocomplete text field items.

That is, when you click an autocomplete suggestion, you will receive the View corresponding to that suggestion along with its index and not the AutocompleteTextView itself.

Use the OnClickListener on your AutocompleteTextViews if you want to be notified when the views themselves are clicked.

If you want to react to changes in text content of your text field you can use the TextWatched listener interface. See TextView API for more information on that.

Kallja
  • 5,362
  • 3
  • 23
  • 33
0

On this statement.

if(view == supplierTextView)
{
        Log.d("Supplier Name selected", supName);
}
else if(view == productTextView)
{
        Log.d("Product Name selected", proName);
}

the type of the view is TextView so nothing happened.

If you want to know which of the 2 AutoCompleteTextView is triggered.

call this statement

supplierTextView.setOnClickListener(listener);

and

productTextView.setOnClickListener(listener);

and in onClick()

public void onClick(View view)
{


 if(view == supplierTextView)
    {
            Log.d("Supplier Name selected", supName);
    }
    else if(view == productTextView)
    {
            Log.d("Product Name selected", proName);
    }

}

don't forget to implement View.OnClickListener

  • can you please elaborate on your answer. how can i know which of the 2 autocomplete widget was selected – ZAJ Jun 19 '12 at 05:56
0

If you want to give click event to auto complete view then change setOnItemClickListener to setOnClickListener. Put if else condition in onClick method. It will work

Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25