2013-12-09 74 views
7

适配器工作正常,但我不明白为什么在OnItemClick位置始终为“0”AutoCompleteTextView OnItemClick位置始终为“0”

String[] regions = ct.getRegions(); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, regions); 
    regionT.setAdapter(adapter); 
    regionT.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      // TODO Auto-generated method stub 
       int pos=position; 
     } 
    }); 

回答

17

不要问我为什么,但在方法OnItemClickListener.onItemClick参数position相对AutoCompleteTextView的下拉列表索引持有,不在你的适配器阵列中的位置(在你的案例regions)!

所以,找到该项目的实际position你得在下拉列表中选择的字符串,找到其索引的转接器阵列中:

String[] regions = ct.getRegions(); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, regions); 
regionT.setAdapter(adapter); 
regionT.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     String selected = (String) parent.getItemAtPosition(position); 
     int pos = Arrays.asList(regions).indexOf(selected); 
    } 
}); 
+1

如果你的'ArrayAdapter's'类型是一个对象,你可以使用'Object object =(Object)parent.getItemAtPosition(position);' – surfer190

+1

来分配它,如果position始终为0,那么这个函数是如何工作的呢? 我和原来的海报有同样的问题。 – user535927

-1

我把这个简单的例子,它能够正常工作为了我。请看下图:

package com.example.autocompletetv; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 

public class AutoCompleteActivity extends ListActivity { 

    public static final String TAG = AutoCompleteActivity.class.getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_auto_complete); 

     String[] regions = {"One", "Two", "Three", "Four", "Five"}; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_dropdown_item_1line, regions); 
     this.setListAdapter(adapter); 

     this.getListView().setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Log.i(TAG, "postion was " + position); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.auto_complete, menu); 
     return true; 
    } 

} 

当我点击我得到:

12-09 19:13:30.617: I/AutoCompleteActivity(1883): postion was 2 
12-09 19:13:31.997: I/AutoCompleteActivity(1883): postion was 3 
12-09 19:13:34.687: I/AutoCompleteActivity(1883): postion was 4 
12-09 19:13:37.028: I/AutoCompleteActivity(1883): postion was 0 
+0

奇怪的是,它并没有在我的应用程序工作 –

+0

你不”包含一整套代码,但可能是regionT不是设置侦听器的正确对象。你在使用ListActivity吗? –