How to add onItemSelectListener in autocomplete Textview

607 views Asked by At

Im trying to add a autocomplete and the set an itemItemSelectListener in that Button clickevent

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,new ArrayList<String>());

   //This is working fine------------------------------------------
    editPhoneNum.setThreshold(1);
    editPhoneNum.setAdapter(adapter);
    editPhoneNum.setOnItemSelectedListener(this);
    editPhoneNum.setOnItemClickListener(this);
  //---------------------------------------------------------------

AddCont_btn.setOnClickListener(new OnClickListener() 
    {
    @Override
    public void onClick(View v) 
    {
      Counter++;
      AutoCompleteTextView = new AutoCompleteTextView(sentsms.this);
      AutoCompleteTextView.setThreshold(1);
      AutoCompleteTextView.setOnItemSelectedListener(sentsms.this); //<--This doesn't work
      AutoCompleteTextView.setAdapter(adapter);
        rel_lay.addView(AutoCompleteTextView);

        }
    });

how to resolve this problem Thanks guys..!!!!

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
    // TODO Auto-generated method stub
    Log.d("AutocompleteContacts"," Name:" + arg0.getItemAtPosition(arg2) + "\n Number:"+ toNumberValue);    
}

XMl rel_lay is a LinearLayout

 <LinearLayout
 android:id="@+id/rel_lay"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:orientation="vertical" >

<AutoCompleteTextView
android:id="@+id/addCont1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Frnds Name"
android:text="" >
</AutoCompleteTextView>
</LinearLayout>
</LinearLayout>

This method is not called when item got selected by AutoCompleteTextView

1

There are 1 answers

9
Arundas K V On BEST ANSWER

remove the autocomplete from the rel_lay from xml file

AddCont_btn.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) 
{
  Counter++;
  AutoCompleteTextView = new AutoCompleteTextView(sentsms.this);
  AutoCompleteTextView.setThreshold(1);
  //make unique id for each autocomplete then only you can refer it individually
  AutoCompleteTextView.setId(your_unique_id);
  AutoCompleteTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  AutoCompleteTextView.setOnItemSelectedListener(sentsms.this); 
  AutoCompleteTextView.setAdapter(adapter);
  rel_lay.addView(AutoCompleteTextView);

    }
});

and do the necessary steps inside the

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  //TODO Auto-generated method stub
 //do here Whatever is your objective on item click

}