android - 如何在autocompleteTextview中添加onItemSelectedListener

标签 android jquery autocomplete onitemselectedlistener

我正在尝试添加一个自动完成功能并在该按钮点击事件中设置一个 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
 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 选择项目时不调用此方法

最佳答案

从 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

}

关于android - 如何在autocompleteTextview中添加onItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23988101/

相关文章:

ruby-on-rails - 使用 rails3-jquery-autocomplete gem 自动完成 active_admin

android - 从 Android 相机 Surface 获取 RGB 值

android - Retrofit 2 请求总是失败 - Kotlin

安卓相机 : stopPreview vs releaseCamera

Django Rest Framework - PUT 到 (many=True) PrimaryKeyRelatedField()

jquery 无法在 'animate' : Partial keyframes are not supported 上执行 'Element'

javascript - 使用 jquery 将值添加到一起

c# - 在 Windows 中的任意位置插入文本。 (如自动图文集/智能感知)

c++ - 如何更改QCompleter的弹出列表的位置?

java - 如何实现适配器并过滤结果?