如何在autocompleteTextview中添加onItemSelectedListener

时间:2014-06-02 05:54:57

标签: android jquery autocomplete onitemselectedlistener

我试图添加一个自动完成并在该按钮clickevent中设置一个itemItemSelectListener

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);

        }
    });

如何解决这个问题谢谢大家.. !!!!

@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是一个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>

当AutoCompleteTextView

选择项目时,不会调用此方法

1 个答案:

答案 0 :(得分:1)

从xml文件

中删除rel_lay中的自动完成功能
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);

    }
});

并在

中执行必要的步骤
@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

}